2 * Copyright (c) 2002-2005, 2009, 2013 Jeffrey Roberson <jeff@FreeBSD.org>
3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4 * Copyright (c) 2004-2006 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * uma_core.c Implementation of the Universal Memory allocator
32 * This allocator is intended to replace the multitude of similar object caches
33 * in the standard FreeBSD kernel. The intent is to be flexible as well as
34 * effecient. A primary design goal is to return unused memory to the rest of
35 * the system. This will make the system as a whole more flexible due to the
36 * ability to move memory to subsystems which most need it instead of leaving
37 * pools of reserved memory unused.
39 * The basic ideas stem from similar slab/zone based allocators whose algorithms
46 * - Improve memory usage for large allocations
47 * - Investigate cache size adjustments
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 /* I should really use ktr.. */
56 #define UMA_DEBUG_ALLOC 1
57 #define UMA_DEBUG_ALLOC_1 1
61 #include "opt_param.h"
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/bitset.h>
67 #include <sys/kernel.h>
68 #include <sys/types.h>
69 #include <sys/queue.h>
70 #include <sys/malloc.h>
73 #include <sys/sysctl.h>
74 #include <sys/mutex.h>
76 #include <sys/rwlock.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;
112 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
115 * The initial hash tables come out of this zone so they can be allocated
116 * prior to malloc coming up.
118 static uma_zone_t hashzone;
120 /* The boot-time adjusted value for cache line alignment. */
121 int uma_align_cache = 64 - 1;
123 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
126 * Are we allowed to allocate buckets?
128 static int bucketdisable = 1;
130 /* Linked list of all kegs in the system */
131 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
133 /* This mutex protects the keg list */
134 static struct mtx_padalign uma_mtx;
136 /* Linked list of boot time pages */
137 static LIST_HEAD(,uma_slab) uma_boot_pages =
138 LIST_HEAD_INITIALIZER(uma_boot_pages);
140 /* This mutex protects the boot time pages list */
141 static struct mtx_padalign uma_boot_pages_mtx;
143 /* Is the VM done starting up? */
144 static int booted = 0;
145 #define UMA_STARTUP 1
146 #define UMA_STARTUP2 2
148 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
149 static const u_int uma_max_ipers = SLAB_SETSIZE;
152 * Only mbuf clusters use ref zones. Just provide enough references
153 * to support the one user. New code should not use the ref facility.
155 static const u_int uma_max_ipers_ref = PAGE_SIZE / MCLBYTES;
158 * This is the handle used to schedule events that need to happen
159 * outside of the allocation fast path.
161 static struct callout uma_callout;
162 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
165 * This structure is passed as the zone ctor arg so that I don't have to create
166 * a special allocation function just for zones.
168 struct uma_zctor_args {
183 struct uma_kctor_args {
192 struct uma_bucket_zone {
195 int ubz_entries; /* Number of items it can hold. */
196 int ubz_maxsize; /* Maximum allocation size per-item. */
200 * Compute the actual number of bucket entries to pack them in power
201 * of two sizes for more efficient space utilization.
203 #define BUCKET_SIZE(n) \
204 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
206 #define BUCKET_MAX BUCKET_SIZE(128)
208 struct uma_bucket_zone bucket_zones[] = {
209 { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
210 { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
211 { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
212 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
213 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
214 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
219 * Flags and enumerations to be passed to internal functions.
221 enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
225 static void *noobj_alloc(uma_zone_t, int, uint8_t *, int);
226 static void *page_alloc(uma_zone_t, int, uint8_t *, int);
227 static void *startup_alloc(uma_zone_t, int, uint8_t *, int);
228 static void page_free(void *, int, uint8_t);
229 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
230 static void cache_drain(uma_zone_t);
231 static void bucket_drain(uma_zone_t, uma_bucket_t);
232 static void bucket_cache_drain(uma_zone_t zone);
233 static int keg_ctor(void *, int, void *, int);
234 static void keg_dtor(void *, int, void *);
235 static int zone_ctor(void *, int, void *, int);
236 static void zone_dtor(void *, int, void *);
237 static int zero_init(void *, int, int);
238 static void keg_small_init(uma_keg_t keg);
239 static void keg_large_init(uma_keg_t keg);
240 static void zone_foreach(void (*zfunc)(uma_zone_t));
241 static void zone_timeout(uma_zone_t zone);
242 static int hash_alloc(struct uma_hash *);
243 static int hash_expand(struct uma_hash *, struct uma_hash *);
244 static void hash_free(struct uma_hash *hash);
245 static void uma_timeout(void *);
246 static void uma_startup3(void);
247 static void *zone_alloc_item(uma_zone_t, void *, int);
248 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
249 static void bucket_enable(void);
250 static void bucket_init(void);
251 static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
252 static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
253 static void bucket_zone_drain(void);
254 static uma_bucket_t zone_alloc_bucket(uma_zone_t zone, void *, int flags);
255 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
256 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
257 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
258 static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
259 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
260 uma_fini fini, int align, uint32_t flags);
261 static int zone_import(uma_zone_t zone, void **bucket, int max, int flags);
262 static void zone_release(uma_zone_t zone, void **bucket, int cnt);
264 void uma_print_zone(uma_zone_t);
265 void uma_print_stats(void);
266 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
267 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
269 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
271 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
272 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
274 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
275 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
277 static int zone_warnings = 1;
278 TUNABLE_INT("vm.zone_warnings", &zone_warnings);
279 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RW, &zone_warnings, 0,
280 "Warn when UMA zones becomes full");
283 * This routine checks to see whether or not it's safe to enable buckets.
288 bucketdisable = vm_page_count_min();
292 * Initialize bucket_zones, the array of zones of buckets of various sizes.
294 * For each zone, calculate the memory required for each bucket, consisting
295 * of the header and an array of pointers.
300 struct uma_bucket_zone *ubz;
304 for (i = 0, ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
305 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
306 size += sizeof(void *) * ubz->ubz_entries;
307 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
308 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
309 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET);
314 * Given a desired number of entries for a bucket, return the zone from which
315 * to allocate the bucket.
317 static struct uma_bucket_zone *
318 bucket_zone_lookup(int entries)
320 struct uma_bucket_zone *ubz;
322 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
323 if (ubz->ubz_entries >= entries)
330 bucket_select(int size)
332 struct uma_bucket_zone *ubz;
334 ubz = &bucket_zones[0];
335 if (size > ubz->ubz_maxsize)
336 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
338 for (; ubz->ubz_entries != 0; ubz++)
339 if (ubz->ubz_maxsize < size)
342 return (ubz->ubz_entries);
346 bucket_alloc(uma_zone_t zone, void *udata, int flags)
348 struct uma_bucket_zone *ubz;
352 * This is to stop us from allocating per cpu buckets while we're
353 * running out of vm.boot_pages. Otherwise, we would exhaust the
354 * boot pages. This also prevents us from allocating buckets in
355 * low memory situations.
360 * To limit bucket recursion we store the original zone flags
361 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
362 * NOVM flag to persist even through deep recursions. We also
363 * store ZFLAG_BUCKET once we have recursed attempting to allocate
364 * a bucket for a bucket zone so we do not allow infinite bucket
365 * recursion. This cookie will even persist to frees of unused
366 * buckets via the allocation path or bucket allocations in the
369 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
371 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
372 udata = (void *)(uintptr_t)zone->uz_flags;
374 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
375 if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
377 ubz = bucket_zone_lookup(zone->uz_count);
378 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
381 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
384 bucket->ub_entries = ubz->ubz_entries;
391 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
393 struct uma_bucket_zone *ubz;
395 KASSERT(bucket->ub_cnt == 0,
396 ("bucket_free: Freeing a non free bucket."));
397 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
398 udata = (void *)(uintptr_t)zone->uz_flags;
399 ubz = bucket_zone_lookup(bucket->ub_entries);
400 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
404 bucket_zone_drain(void)
406 struct uma_bucket_zone *ubz;
408 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
409 zone_drain(ubz->ubz_zone);
413 zone_log_warning(uma_zone_t zone)
415 static const struct timeval warninterval = { 300, 0 };
417 if (!zone_warnings || zone->uz_warning == NULL)
420 if (ratecheck(&zone->uz_ratecheck, &warninterval))
421 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
425 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
429 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
430 kegfn(klink->kl_keg);
434 * Routine called by timeout which is used to fire off some time interval
435 * based calculations. (stats, hash size, etc.)
444 uma_timeout(void *unused)
447 zone_foreach(zone_timeout);
449 /* Reschedule this event */
450 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
454 * Routine to perform timeout driven calculations. This expands the
455 * hashes and does per cpu statistics aggregation.
460 keg_timeout(uma_keg_t keg)
465 * Expand the keg hash table.
467 * This is done if the number of slabs is larger than the hash size.
468 * What I'm trying to do here is completely reduce collisions. This
469 * may be a little aggressive. Should I allow for two collisions max?
471 if (keg->uk_flags & UMA_ZONE_HASH &&
472 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
473 struct uma_hash newhash;
474 struct uma_hash oldhash;
478 * This is so involved because allocating and freeing
479 * while the keg lock is held will lead to deadlock.
480 * I have to do everything in stages and check for
483 newhash = keg->uk_hash;
485 ret = hash_alloc(&newhash);
488 if (hash_expand(&keg->uk_hash, &newhash)) {
489 oldhash = keg->uk_hash;
490 keg->uk_hash = newhash;
503 zone_timeout(uma_zone_t zone)
506 zone_foreach_keg(zone, &keg_timeout);
510 * Allocate and zero fill the next sized hash table from the appropriate
514 * hash A new hash structure with the old hash size in uh_hashsize
517 * 1 on sucess and 0 on failure.
520 hash_alloc(struct uma_hash *hash)
525 oldsize = hash->uh_hashsize;
527 /* We're just going to go to a power of two greater */
529 hash->uh_hashsize = oldsize * 2;
530 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
531 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
532 M_UMAHASH, M_NOWAIT);
534 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
535 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
537 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
539 if (hash->uh_slab_hash) {
540 bzero(hash->uh_slab_hash, alloc);
541 hash->uh_hashmask = hash->uh_hashsize - 1;
549 * Expands the hash table for HASH zones. This is done from zone_timeout
550 * to reduce collisions. This must not be done in the regular allocation
551 * path, otherwise, we can recurse on the vm while allocating pages.
554 * oldhash The hash you want to expand
555 * newhash The hash structure for the new table
563 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
569 if (!newhash->uh_slab_hash)
572 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
576 * I need to investigate hash algorithms for resizing without a
580 for (i = 0; i < oldhash->uh_hashsize; i++)
581 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
582 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
583 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
584 hval = UMA_HASH(newhash, slab->us_data);
585 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
593 * Free the hash bucket to the appropriate backing store.
596 * slab_hash The hash bucket we're freeing
597 * hashsize The number of entries in that hash bucket
603 hash_free(struct uma_hash *hash)
605 if (hash->uh_slab_hash == NULL)
607 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
608 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
610 free(hash->uh_slab_hash, M_UMAHASH);
614 * Frees all outstanding items in a bucket
617 * zone The zone to free to, must be unlocked.
618 * bucket The free/alloc bucket with items, cpu queue must be locked.
625 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
633 for (i = 0; i < bucket->ub_cnt; i++)
634 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
635 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
640 * Drains the per cpu caches for a zone.
642 * NOTE: This may only be called while the zone is being turn down, and not
643 * during normal operation. This is necessary in order that we do not have
644 * to migrate CPUs to drain the per-CPU caches.
647 * zone The zone to drain, must be unlocked.
653 cache_drain(uma_zone_t zone)
659 * XXX: It is safe to not lock the per-CPU caches, because we're
660 * tearing down the zone anyway. I.e., there will be no further use
661 * of the caches at this point.
663 * XXX: It would good to be able to assert that the zone is being
664 * torn down to prevent improper use of cache_drain().
666 * XXX: We lock the zone before passing into bucket_cache_drain() as
667 * it is used elsewhere. Should the tear-down path be made special
668 * there in some form?
671 cache = &zone->uz_cpu[cpu];
672 bucket_drain(zone, cache->uc_allocbucket);
673 bucket_drain(zone, cache->uc_freebucket);
674 if (cache->uc_allocbucket != NULL)
675 bucket_free(zone, cache->uc_allocbucket, NULL);
676 if (cache->uc_freebucket != NULL)
677 bucket_free(zone, cache->uc_freebucket, NULL);
678 cache->uc_allocbucket = cache->uc_freebucket = NULL;
681 bucket_cache_drain(zone);
686 * Drain the cached buckets from a zone. Expects a locked zone on entry.
689 bucket_cache_drain(uma_zone_t zone)
694 * Drain the bucket queues and free the buckets, we just keep two per
697 while ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
698 LIST_REMOVE(bucket, ub_link);
700 bucket_drain(zone, bucket);
701 bucket_free(zone, bucket, NULL);
707 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
714 flags = slab->us_flags;
716 if (keg->uk_fini != NULL) {
717 for (i--; i > -1; i--)
718 keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
721 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
722 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
724 printf("%s: Returning %d bytes.\n", keg->uk_name,
725 PAGE_SIZE * keg->uk_ppera);
727 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
731 * Frees pages from a keg back to the system. This is done on demand from
732 * the pageout daemon.
737 keg_drain(uma_keg_t keg)
739 struct slabhead freeslabs = { 0 };
744 * We don't want to take pages from statically allocated kegs at this
747 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
751 printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
754 if (keg->uk_free == 0)
757 slab = LIST_FIRST(&keg->uk_free_slab);
759 n = LIST_NEXT(slab, us_link);
761 /* We have no where to free these to */
762 if (slab->us_flags & UMA_SLAB_BOOT) {
767 LIST_REMOVE(slab, us_link);
768 keg->uk_pages -= keg->uk_ppera;
769 keg->uk_free -= keg->uk_ipers;
771 if (keg->uk_flags & UMA_ZONE_HASH)
772 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
774 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
781 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
782 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
783 keg_free_slab(keg, slab, keg->uk_ipers);
788 zone_drain_wait(uma_zone_t zone, int waitok)
792 * Set draining to interlock with zone_dtor() so we can release our
793 * locks as we go. Only dtor() should do a WAITOK call since it
794 * is the only call that knows the structure will still be available
798 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
799 if (waitok == M_NOWAIT)
801 mtx_unlock(&uma_mtx);
802 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
805 zone->uz_flags |= UMA_ZFLAG_DRAINING;
806 bucket_cache_drain(zone);
809 * The DRAINING flag protects us from being freed while
810 * we're running. Normally the uma_mtx would protect us but we
811 * must be able to release and acquire the right lock for each keg.
813 zone_foreach_keg(zone, &keg_drain);
815 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
822 zone_drain(uma_zone_t zone)
825 zone_drain_wait(zone, M_NOWAIT);
829 * Allocate a new slab for a keg. This does not insert the slab onto a list.
832 * wait Shall we wait?
835 * The slab that was allocated or NULL if there is no memory and the
836 * caller specified M_NOWAIT.
839 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
841 uma_slabrefcnt_t slabref;
848 mtx_assert(&keg->uk_lock, MA_OWNED);
853 printf("alloc_slab: Allocating a new slab for %s\n", keg->uk_name);
855 allocf = keg->uk_allocf;
858 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
859 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
865 * This reproduces the old vm_zone behavior of zero filling pages the
866 * first time they are added to a zone.
868 * Malloced items are zeroed in uma_zalloc.
871 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
876 if (keg->uk_flags & UMA_ZONE_NODUMP)
879 /* zone is passed for legacy reasons. */
880 mem = allocf(zone, keg->uk_ppera * PAGE_SIZE, &flags, wait);
882 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
883 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
888 /* Point the slab into the allocated memory */
889 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
890 slab = (uma_slab_t )(mem + keg->uk_pgoff);
892 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
893 for (i = 0; i < keg->uk_ppera; i++)
894 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
898 slab->us_freecount = keg->uk_ipers;
899 slab->us_flags = flags;
900 BIT_FILL(SLAB_SETSIZE, &slab->us_free);
902 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
904 if (keg->uk_flags & UMA_ZONE_REFCNT) {
905 slabref = (uma_slabrefcnt_t)slab;
906 for (i = 0; i < keg->uk_ipers; i++)
907 slabref->us_refcnt[i] = 0;
910 if (keg->uk_init != NULL) {
911 for (i = 0; i < keg->uk_ipers; i++)
912 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
913 keg->uk_size, wait) != 0)
915 if (i != keg->uk_ipers) {
916 keg_free_slab(keg, slab, i);
925 if (keg->uk_flags & UMA_ZONE_HASH)
926 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
928 keg->uk_pages += keg->uk_ppera;
929 keg->uk_free += keg->uk_ipers;
936 * This function is intended to be used early on in place of page_alloc() so
937 * that we may use the boot time page cache to satisfy allocations before
941 startup_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
945 int pages, check_pages;
947 keg = zone_first_keg(zone);
948 pages = howmany(bytes, PAGE_SIZE);
949 check_pages = pages - 1;
950 KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
953 * Check our small startup cache to see if it has pages remaining.
955 mtx_lock(&uma_boot_pages_mtx);
957 /* First check if we have enough room. */
958 tmps = LIST_FIRST(&uma_boot_pages);
959 while (tmps != NULL && check_pages-- > 0)
960 tmps = LIST_NEXT(tmps, us_link);
963 * It's ok to lose tmps references. The last one will
964 * have tmps->us_data pointing to the start address of
965 * "pages" contiguous pages of memory.
967 while (pages-- > 0) {
968 tmps = LIST_FIRST(&uma_boot_pages);
969 LIST_REMOVE(tmps, us_link);
971 mtx_unlock(&uma_boot_pages_mtx);
972 *pflag = tmps->us_flags;
973 return (tmps->us_data);
975 mtx_unlock(&uma_boot_pages_mtx);
976 if (booted < UMA_STARTUP2)
977 panic("UMA: Increase vm.boot_pages");
979 * Now that we've booted reset these users to their real allocator.
981 #ifdef UMA_MD_SMALL_ALLOC
982 keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
984 keg->uk_allocf = page_alloc;
986 return keg->uk_allocf(zone, bytes, pflag, wait);
990 * Allocates a number of pages from the system
993 * bytes The number of bytes requested
994 * wait Shall we wait?
997 * A pointer to the alloced memory or possibly
998 * NULL if M_NOWAIT is set.
1001 page_alloc(uma_zone_t zone, int bytes, uint8_t *pflag, int wait)
1003 void *p; /* Returned page */
1005 *pflag = UMA_SLAB_KMEM;
1006 p = (void *) kmem_malloc(kmem_arena, bytes, wait);
1012 * Allocates a number of pages from within an object
1015 * bytes The number of bytes requested
1016 * wait Shall we wait?
1019 * A pointer to the alloced memory or possibly
1020 * NULL if M_NOWAIT is set.
1023 noobj_alloc(uma_zone_t zone, int bytes, uint8_t *flags, int wait)
1025 TAILQ_HEAD(, vm_page) alloctail;
1027 vm_offset_t retkva, zkva;
1028 vm_page_t p, p_next;
1031 TAILQ_INIT(&alloctail);
1032 keg = zone_first_keg(zone);
1034 npages = howmany(bytes, PAGE_SIZE);
1035 while (npages > 0) {
1036 p = vm_page_alloc(NULL, 0, VM_ALLOC_INTERRUPT |
1037 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ);
1040 * Since the page does not belong to an object, its
1043 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1047 if (wait & M_WAITOK) {
1053 * Page allocation failed, free intermediate pages and
1056 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1057 vm_page_unwire(p, 0);
1062 *flags = UMA_SLAB_PRIV;
1063 zkva = keg->uk_kva +
1064 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1066 TAILQ_FOREACH(p, &alloctail, listq) {
1067 pmap_qenter(zkva, &p, 1);
1071 return ((void *)retkva);
1075 * Frees a number of pages to the system
1078 * mem A pointer to the memory to be freed
1079 * size The size of the memory being freed
1080 * flags The original p->us_flags field
1086 page_free(void *mem, int size, uint8_t flags)
1090 if (flags & UMA_SLAB_KMEM)
1092 else if (flags & UMA_SLAB_KERNEL)
1093 vmem = kernel_arena;
1095 panic("UMA: page_free used with invalid flags %d", flags);
1097 kmem_free(vmem, (vm_offset_t)mem, size);
1101 * Zero fill initializer
1103 * Arguments/Returns follow uma_init specifications
1106 zero_init(void *mem, int size, int flags)
1113 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1116 * keg The zone we should initialize
1122 keg_small_init(uma_keg_t keg)
1129 if (keg->uk_flags & UMA_ZONE_PCPU) {
1130 u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
1132 keg->uk_slabsize = sizeof(struct pcpu);
1133 keg->uk_ppera = howmany(ncpus * sizeof(struct pcpu),
1136 keg->uk_slabsize = UMA_SLAB_SIZE;
1141 * Calculate the size of each allocation (rsize) according to
1142 * alignment. If the requested size is smaller than we have
1143 * allocation bits for we round it up.
1145 rsize = keg->uk_size;
1146 if (rsize < keg->uk_slabsize / SLAB_SETSIZE)
1147 rsize = keg->uk_slabsize / SLAB_SETSIZE;
1148 if (rsize & keg->uk_align)
1149 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1150 keg->uk_rsize = rsize;
1152 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1153 keg->uk_rsize < sizeof(struct pcpu),
1154 ("%s: size %u too large", __func__, keg->uk_rsize));
1156 if (keg->uk_flags & UMA_ZONE_REFCNT)
1157 rsize += sizeof(uint32_t);
1159 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1162 shsize = sizeof(struct uma_slab);
1164 keg->uk_ipers = (keg->uk_slabsize - shsize) / rsize;
1165 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1166 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1168 memused = keg->uk_ipers * rsize + shsize;
1169 wastedspace = keg->uk_slabsize - memused;
1172 * We can't do OFFPAGE if we're internal or if we've been
1173 * asked to not go to the VM for buckets. If we do this we
1174 * may end up going to the VM for slabs which we do not
1175 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
1176 * of UMA_ZONE_VM, which clearly forbids it.
1178 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1179 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1183 * See if using an OFFPAGE slab will limit our waste. Only do
1184 * this if it permits more items per-slab.
1186 * XXX We could try growing slabsize to limit max waste as well.
1187 * Historically this was not done because the VM could not
1188 * efficiently handle contiguous allocations.
1190 if ((wastedspace >= keg->uk_slabsize / UMA_MAX_WASTE) &&
1191 (keg->uk_ipers < (keg->uk_slabsize / keg->uk_rsize))) {
1192 keg->uk_ipers = keg->uk_slabsize / keg->uk_rsize;
1193 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1194 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1196 printf("UMA decided we need offpage slab headers for "
1197 "keg: %s, calculated wastedspace = %d, "
1198 "maximum wasted space allowed = %d, "
1199 "calculated ipers = %d, "
1200 "new wasted space = %d\n", keg->uk_name, wastedspace,
1201 keg->uk_slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1202 keg->uk_slabsize - keg->uk_ipers * keg->uk_rsize);
1204 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1207 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1208 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1209 keg->uk_flags |= UMA_ZONE_HASH;
1213 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1214 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1218 * keg The keg we should initialize
1224 keg_large_init(uma_keg_t keg)
1227 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1228 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1229 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1230 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1231 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1233 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1234 keg->uk_slabsize = keg->uk_ppera * PAGE_SIZE;
1236 keg->uk_rsize = keg->uk_size;
1238 /* We can't do OFFPAGE if we're internal, bail out here. */
1239 if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1242 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1243 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1244 keg->uk_flags |= UMA_ZONE_HASH;
1248 keg_cachespread_init(uma_keg_t keg)
1255 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1256 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1258 alignsize = keg->uk_align + 1;
1259 rsize = keg->uk_size;
1261 * We want one item to start on every align boundary in a page. To
1262 * do this we will span pages. We will also extend the item by the
1263 * size of align if it is an even multiple of align. Otherwise, it
1264 * would fall on the same boundary every time.
1266 if (rsize & keg->uk_align)
1267 rsize = (rsize & ~keg->uk_align) + alignsize;
1268 if ((rsize & alignsize) == 0)
1270 trailer = rsize - keg->uk_size;
1271 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1272 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1273 keg->uk_rsize = rsize;
1274 keg->uk_ppera = pages;
1275 keg->uk_slabsize = UMA_SLAB_SIZE;
1276 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1277 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1278 KASSERT(keg->uk_ipers <= uma_max_ipers,
1279 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1284 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1285 * the keg onto the global keg list.
1287 * Arguments/Returns follow uma_ctor specifications
1288 * udata Actually uma_kctor_args
1291 keg_ctor(void *mem, int size, void *udata, int flags)
1293 struct uma_kctor_args *arg = udata;
1294 uma_keg_t keg = mem;
1298 keg->uk_size = arg->size;
1299 keg->uk_init = arg->uminit;
1300 keg->uk_fini = arg->fini;
1301 keg->uk_align = arg->align;
1303 keg->uk_reserve = 0;
1305 keg->uk_flags = arg->flags;
1306 keg->uk_allocf = page_alloc;
1307 keg->uk_freef = page_free;
1308 keg->uk_slabzone = NULL;
1311 * The master zone is passed to us at keg-creation time.
1314 keg->uk_name = zone->uz_name;
1316 if (arg->flags & UMA_ZONE_VM)
1317 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1319 if (arg->flags & UMA_ZONE_ZINIT)
1320 keg->uk_init = zero_init;
1322 if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1323 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1325 if (arg->flags & UMA_ZONE_PCPU)
1327 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1329 keg->uk_flags &= ~UMA_ZONE_PCPU;
1332 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1333 keg_cachespread_init(keg);
1334 } else if (keg->uk_flags & UMA_ZONE_REFCNT) {
1336 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) -
1338 keg_large_init(keg);
1340 keg_small_init(keg);
1342 if (keg->uk_size > (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1343 keg_large_init(keg);
1345 keg_small_init(keg);
1348 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1349 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1350 if (keg->uk_ipers > uma_max_ipers_ref)
1351 panic("Too many ref items per zone: %d > %d\n",
1352 keg->uk_ipers, uma_max_ipers_ref);
1353 keg->uk_slabzone = slabrefzone;
1355 keg->uk_slabzone = slabzone;
1359 * If we haven't booted yet we need allocations to go through the
1360 * startup cache until the vm is ready.
1362 if (keg->uk_ppera == 1) {
1363 #ifdef UMA_MD_SMALL_ALLOC
1364 keg->uk_allocf = uma_small_alloc;
1365 keg->uk_freef = uma_small_free;
1367 if (booted < UMA_STARTUP)
1368 keg->uk_allocf = startup_alloc;
1370 if (booted < UMA_STARTUP2)
1371 keg->uk_allocf = startup_alloc;
1373 } else if (booted < UMA_STARTUP2 &&
1374 (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1375 keg->uk_allocf = startup_alloc;
1378 * Initialize keg's lock
1380 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1383 * If we're putting the slab header in the actual page we need to
1384 * figure out where in each page it goes. This calculates a right
1385 * justified offset into the memory on an ALIGN_PTR boundary.
1387 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1390 /* Size of the slab struct and free list */
1391 totsize = sizeof(struct uma_slab);
1393 /* Size of the reference counts. */
1394 if (keg->uk_flags & UMA_ZONE_REFCNT)
1395 totsize += keg->uk_ipers * sizeof(uint32_t);
1397 if (totsize & UMA_ALIGN_PTR)
1398 totsize = (totsize & ~UMA_ALIGN_PTR) +
1399 (UMA_ALIGN_PTR + 1);
1400 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1403 * The only way the following is possible is if with our
1404 * UMA_ALIGN_PTR adjustments we are now bigger than
1405 * UMA_SLAB_SIZE. I haven't checked whether this is
1406 * mathematically possible for all cases, so we make
1409 totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1410 if (keg->uk_flags & UMA_ZONE_REFCNT)
1411 totsize += keg->uk_ipers * sizeof(uint32_t);
1412 if (totsize > PAGE_SIZE * keg->uk_ppera) {
1413 printf("zone %s ipers %d rsize %d size %d\n",
1414 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1416 panic("UMA slab won't fit.");
1420 if (keg->uk_flags & UMA_ZONE_HASH)
1421 hash_alloc(&keg->uk_hash);
1424 printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1425 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1426 keg->uk_ipers, keg->uk_ppera,
1427 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1430 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1433 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1434 mtx_unlock(&uma_mtx);
1439 * Zone header ctor. This initializes all fields, locks, etc.
1441 * Arguments/Returns follow uma_ctor specifications
1442 * udata Actually uma_zctor_args
1445 zone_ctor(void *mem, int size, void *udata, int flags)
1447 struct uma_zctor_args *arg = udata;
1448 uma_zone_t zone = mem;
1453 zone->uz_name = arg->name;
1454 zone->uz_ctor = arg->ctor;
1455 zone->uz_dtor = arg->dtor;
1456 zone->uz_slab = zone_fetch_slab;
1457 zone->uz_init = NULL;
1458 zone->uz_fini = NULL;
1459 zone->uz_allocs = 0;
1462 zone->uz_sleeps = 0;
1465 zone->uz_warning = NULL;
1466 timevalclear(&zone->uz_ratecheck);
1469 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1472 * This is a pure cache zone, no kegs.
1475 if (arg->flags & UMA_ZONE_VM)
1476 arg->flags |= UMA_ZFLAG_CACHEONLY;
1477 zone->uz_flags = arg->flags;
1478 zone->uz_size = arg->size;
1479 zone->uz_import = arg->import;
1480 zone->uz_release = arg->release;
1481 zone->uz_arg = arg->arg;
1482 zone->uz_lockptr = &zone->uz_lock;
1487 * Use the regular zone/keg/slab allocator.
1489 zone->uz_import = (uma_import)zone_import;
1490 zone->uz_release = (uma_release)zone_release;
1491 zone->uz_arg = zone;
1493 if (arg->flags & UMA_ZONE_SECONDARY) {
1494 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1495 zone->uz_init = arg->uminit;
1496 zone->uz_fini = arg->fini;
1497 zone->uz_lockptr = &keg->uk_lock;
1498 zone->uz_flags |= UMA_ZONE_SECONDARY;
1501 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1502 if (LIST_NEXT(z, uz_link) == NULL) {
1503 LIST_INSERT_AFTER(z, zone, uz_link);
1508 mtx_unlock(&uma_mtx);
1509 } else if (keg == NULL) {
1510 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1511 arg->align, arg->flags)) == NULL)
1514 struct uma_kctor_args karg;
1517 /* We should only be here from uma_startup() */
1518 karg.size = arg->size;
1519 karg.uminit = arg->uminit;
1520 karg.fini = arg->fini;
1521 karg.align = arg->align;
1522 karg.flags = arg->flags;
1524 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1531 * Link in the first keg.
1533 zone->uz_klink.kl_keg = keg;
1534 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1535 zone->uz_lockptr = &keg->uk_lock;
1536 zone->uz_size = keg->uk_size;
1537 zone->uz_flags |= (keg->uk_flags &
1538 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1541 * Some internal zones don't have room allocated for the per cpu
1542 * caches. If we're internal, bail out here.
1544 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1545 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1546 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1551 if ((arg->flags & UMA_ZONE_MAXBUCKET) == 0)
1552 zone->uz_count = bucket_select(zone->uz_size);
1554 zone->uz_count = BUCKET_MAX;
1560 * Keg header dtor. This frees all data, destroys locks, frees the hash
1561 * table and removes the keg from the global list.
1563 * Arguments/Returns follow uma_dtor specifications
1567 keg_dtor(void *arg, int size, void *udata)
1571 keg = (uma_keg_t)arg;
1573 if (keg->uk_free != 0) {
1574 printf("Freed UMA keg (%s) was not empty (%d items). "
1575 " Lost %d pages of memory.\n",
1576 keg->uk_name ? keg->uk_name : "",
1577 keg->uk_free, keg->uk_pages);
1581 hash_free(&keg->uk_hash);
1589 * Arguments/Returns follow uma_dtor specifications
1593 zone_dtor(void *arg, int size, void *udata)
1599 zone = (uma_zone_t)arg;
1600 keg = zone_first_keg(zone);
1602 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1606 LIST_REMOVE(zone, uz_link);
1607 mtx_unlock(&uma_mtx);
1609 * XXX there are some races here where
1610 * the zone can be drained but zone lock
1611 * released and then refilled before we
1612 * remove it... we dont care for now
1614 zone_drain_wait(zone, M_WAITOK);
1616 * Unlink all of our kegs.
1618 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1619 klink->kl_keg = NULL;
1620 LIST_REMOVE(klink, kl_link);
1621 if (klink == &zone->uz_klink)
1623 free(klink, M_TEMP);
1626 * We only destroy kegs from non secondary zones.
1628 if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1630 LIST_REMOVE(keg, uk_link);
1631 mtx_unlock(&uma_mtx);
1632 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1634 ZONE_LOCK_FINI(zone);
1638 * Traverses every zone in the system and calls a callback
1641 * zfunc A pointer to a function which accepts a zone
1648 zone_foreach(void (*zfunc)(uma_zone_t))
1654 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1655 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1658 mtx_unlock(&uma_mtx);
1661 /* Public functions */
1664 uma_startup(void *bootmem, int boot_pages)
1666 struct uma_zctor_args args;
1672 printf("Creating uma keg headers zone and keg.\n");
1674 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1676 /* "manually" create the initial zone */
1677 memset(&args, 0, sizeof(args));
1678 args.name = "UMA Kegs";
1679 args.size = sizeof(struct uma_keg);
1680 args.ctor = keg_ctor;
1681 args.dtor = keg_dtor;
1682 args.uminit = zero_init;
1684 args.keg = &masterkeg;
1685 args.align = 32 - 1;
1686 args.flags = UMA_ZFLAG_INTERNAL;
1687 /* The initial zone has no Per cpu queues so it's smaller */
1688 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1691 printf("Filling boot free list.\n");
1693 for (i = 0; i < boot_pages; i++) {
1694 slab = (uma_slab_t)((uint8_t *)bootmem + (i * UMA_SLAB_SIZE));
1695 slab->us_data = (uint8_t *)slab;
1696 slab->us_flags = UMA_SLAB_BOOT;
1697 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1699 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1702 printf("Creating uma zone headers zone and keg.\n");
1704 args.name = "UMA Zones";
1705 args.size = sizeof(struct uma_zone) +
1706 (sizeof(struct uma_cache) * (mp_maxid + 1));
1707 args.ctor = zone_ctor;
1708 args.dtor = zone_dtor;
1709 args.uminit = zero_init;
1712 args.align = 32 - 1;
1713 args.flags = UMA_ZFLAG_INTERNAL;
1714 /* The initial zone has no Per cpu queues so it's smaller */
1715 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1718 printf("Initializing pcpu cache locks.\n");
1721 printf("Creating slab and hash zones.\n");
1724 /* Now make a zone for slab headers */
1725 slabzone = uma_zcreate("UMA Slabs",
1726 sizeof(struct uma_slab),
1727 NULL, NULL, NULL, NULL,
1728 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1731 * We also create a zone for the bigger slabs with reference
1732 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1734 slabsize = sizeof(struct uma_slab_refcnt);
1735 slabsize += uma_max_ipers_ref * sizeof(uint32_t);
1736 slabrefzone = uma_zcreate("UMA RCntSlabs",
1738 NULL, NULL, NULL, NULL,
1740 UMA_ZFLAG_INTERNAL);
1742 hashzone = uma_zcreate("UMA Hash",
1743 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1744 NULL, NULL, NULL, NULL,
1745 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1749 booted = UMA_STARTUP;
1752 printf("UMA startup complete.\n");
1760 booted = UMA_STARTUP2;
1763 printf("UMA startup2 complete.\n");
1768 * Initialize our callout handle
1776 printf("Starting callout.\n");
1778 callout_init(&uma_callout, CALLOUT_MPSAFE);
1779 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1781 printf("UMA startup3 complete.\n");
1786 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1787 int align, uint32_t flags)
1789 struct uma_kctor_args args;
1792 args.uminit = uminit;
1794 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1797 return (zone_alloc_item(kegs, &args, M_WAITOK));
1802 uma_set_align(int align)
1805 if (align != UMA_ALIGN_CACHE)
1806 uma_align_cache = align;
1811 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1812 uma_init uminit, uma_fini fini, int align, uint32_t flags)
1815 struct uma_zctor_args args;
1817 /* This stuff is essential for the zone ctor */
1818 memset(&args, 0, sizeof(args));
1823 args.uminit = uminit;
1829 return (zone_alloc_item(zones, &args, M_WAITOK));
1834 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1835 uma_init zinit, uma_fini zfini, uma_zone_t master)
1837 struct uma_zctor_args args;
1840 keg = zone_first_keg(master);
1841 memset(&args, 0, sizeof(args));
1843 args.size = keg->uk_size;
1846 args.uminit = zinit;
1848 args.align = keg->uk_align;
1849 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1852 /* XXX Attaches only one keg of potentially many. */
1853 return (zone_alloc_item(zones, &args, M_WAITOK));
1858 uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
1859 uma_init zinit, uma_fini zfini, uma_import zimport,
1860 uma_release zrelease, void *arg, int flags)
1862 struct uma_zctor_args args;
1864 memset(&args, 0, sizeof(args));
1869 args.uminit = zinit;
1871 args.import = zimport;
1872 args.release = zrelease;
1877 return (zone_alloc_item(zones, &args, M_WAITOK));
1881 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1885 mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
1888 mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
1893 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1901 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1908 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1910 zone_lock_pair(zone, master);
1912 * zone must use vtoslab() to resolve objects and must already be
1915 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1916 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1921 * The new master must also use vtoslab().
1923 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1928 * Both must either be refcnt, or not be refcnt.
1930 if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1931 (master->uz_flags & UMA_ZONE_REFCNT)) {
1936 * The underlying object must be the same size. rsize
1939 if (master->uz_size != zone->uz_size) {
1944 * Put it at the end of the list.
1946 klink->kl_keg = zone_first_keg(master);
1947 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1948 if (LIST_NEXT(kl, kl_link) == NULL) {
1949 LIST_INSERT_AFTER(kl, klink, kl_link);
1954 zone->uz_flags |= UMA_ZFLAG_MULTI;
1955 zone->uz_slab = zone_fetch_slab_multi;
1958 zone_unlock_pair(zone, master);
1960 free(klink, M_TEMP);
1968 uma_zdestroy(uma_zone_t zone)
1971 zone_free_item(zones, zone, NULL, SKIP_NONE);
1976 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1980 uma_bucket_t bucket;
1984 /* This is the fast path allocation */
1985 #ifdef UMA_DEBUG_ALLOC_1
1986 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1988 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1989 zone->uz_name, flags);
1991 if (flags & M_WAITOK) {
1992 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1993 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1995 #ifdef DEBUG_MEMGUARD
1996 if (memguard_cmp_zone(zone)) {
1997 item = memguard_alloc(zone->uz_size, flags);
2000 * Avoid conflict with the use-after-free
2001 * protecting infrastructure from INVARIANTS.
2003 if (zone->uz_init != NULL &&
2004 zone->uz_init != mtrash_init &&
2005 zone->uz_init(item, zone->uz_size, flags) != 0)
2007 if (zone->uz_ctor != NULL &&
2008 zone->uz_ctor != mtrash_ctor &&
2009 zone->uz_ctor(item, zone->uz_size, udata,
2011 zone->uz_fini(item, zone->uz_size);
2016 /* This is unfortunate but should not be fatal. */
2020 * If possible, allocate from the per-CPU cache. There are two
2021 * requirements for safe access to the per-CPU cache: (1) the thread
2022 * accessing the cache must not be preempted or yield during access,
2023 * and (2) the thread must not migrate CPUs without switching which
2024 * cache it accesses. We rely on a critical section to prevent
2025 * preemption and migration. We release the critical section in
2026 * order to acquire the zone mutex if we are unable to allocate from
2027 * the current cache; when we re-acquire the critical section, we
2028 * must detect and handle migration if it has occurred.
2032 cache = &zone->uz_cpu[cpu];
2035 bucket = cache->uc_allocbucket;
2036 if (bucket != NULL && bucket->ub_cnt > 0) {
2038 item = bucket->ub_bucket[bucket->ub_cnt];
2040 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2042 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2045 if (zone->uz_ctor != NULL &&
2046 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2047 atomic_add_long(&zone->uz_fails, 1);
2048 zone_free_item(zone, item, udata, SKIP_DTOR);
2052 uma_dbg_alloc(zone, NULL, item);
2055 bzero(item, zone->uz_size);
2060 * We have run out of items in our alloc bucket.
2061 * See if we can switch with our free bucket.
2063 bucket = cache->uc_freebucket;
2064 if (bucket != NULL && bucket->ub_cnt > 0) {
2065 #ifdef UMA_DEBUG_ALLOC
2066 printf("uma_zalloc: Swapping empty with alloc.\n");
2068 cache->uc_freebucket = cache->uc_allocbucket;
2069 cache->uc_allocbucket = bucket;
2074 * Discard any empty allocation bucket while we hold no locks.
2076 bucket = cache->uc_allocbucket;
2077 cache->uc_allocbucket = NULL;
2080 bucket_free(zone, bucket, udata);
2082 /* Short-circuit for zones without buckets and low memory. */
2083 if (zone->uz_count == 0 || bucketdisable)
2087 * Attempt to retrieve the item from the per-CPU cache has failed, so
2088 * we must go back to the zone. This requires the zone lock, so we
2089 * must drop the critical section, then re-acquire it when we go back
2090 * to the cache. Since the critical section is released, we may be
2091 * preempted or migrate. As such, make sure not to maintain any
2092 * thread-local state specific to the cache from prior to releasing
2093 * the critical section.
2096 if (ZONE_TRYLOCK(zone) == 0) {
2097 /* Record contention to size the buckets. */
2103 cache = &zone->uz_cpu[cpu];
2106 * Since we have locked the zone we may as well send back our stats.
2108 atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
2109 atomic_add_long(&zone->uz_frees, cache->uc_frees);
2110 cache->uc_allocs = 0;
2111 cache->uc_frees = 0;
2113 /* See if we lost the race to fill the cache. */
2114 if (cache->uc_allocbucket != NULL) {
2120 * Check the zone's cache of buckets.
2122 if ((bucket = LIST_FIRST(&zone->uz_buckets)) != NULL) {
2123 KASSERT(bucket->ub_cnt != 0,
2124 ("uma_zalloc_arg: Returning an empty bucket."));
2126 LIST_REMOVE(bucket, ub_link);
2127 cache->uc_allocbucket = bucket;
2131 /* We are no longer associated with this CPU. */
2135 * We bump the uz count when the cache size is insufficient to
2136 * handle the working set.
2138 if (lockfail && zone->uz_count < BUCKET_MAX)
2143 * Now lets just fill a bucket and put it on the free list. If that
2144 * works we'll restart the allocation from the begining and it
2145 * will use the just filled bucket.
2147 bucket = zone_alloc_bucket(zone, udata, flags);
2148 if (bucket != NULL) {
2152 cache = &zone->uz_cpu[cpu];
2154 * See if we lost the race or were migrated. Cache the
2155 * initialized bucket to make this less likely or claim
2156 * the memory directly.
2158 if (cache->uc_allocbucket == NULL)
2159 cache->uc_allocbucket = bucket;
2161 LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2167 * We may not be able to get a bucket so return an actual item.
2170 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2174 item = zone_alloc_item(zone, udata, flags);
2180 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2185 mtx_assert(&keg->uk_lock, MA_OWNED);
2188 if ((flags & M_USE_RESERVE) == 0)
2189 reserve = keg->uk_reserve;
2193 * Find a slab with some space. Prefer slabs that are partially
2194 * used over those that are totally full. This helps to reduce
2197 if (keg->uk_free > reserve) {
2198 if (!LIST_EMPTY(&keg->uk_part_slab)) {
2199 slab = LIST_FIRST(&keg->uk_part_slab);
2201 slab = LIST_FIRST(&keg->uk_free_slab);
2202 LIST_REMOVE(slab, us_link);
2203 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2206 MPASS(slab->us_keg == keg);
2211 * M_NOVM means don't ask at all!
2216 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2217 keg->uk_flags |= UMA_ZFLAG_FULL;
2219 * If this is not a multi-zone, set the FULL bit.
2220 * Otherwise slab_multi() takes care of it.
2222 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2223 zone->uz_flags |= UMA_ZFLAG_FULL;
2224 zone_log_warning(zone);
2226 if (flags & M_NOWAIT)
2229 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2232 slab = keg_alloc_slab(keg, zone, flags);
2234 * If we got a slab here it's safe to mark it partially used
2235 * and return. We assume that the caller is going to remove
2236 * at least one item.
2239 MPASS(slab->us_keg == keg);
2240 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2244 * We might not have been able to get a slab but another cpu
2245 * could have while we were unlocked. Check again before we
2254 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2259 keg = zone_first_keg(zone);
2264 slab = keg_fetch_slab(keg, zone, flags);
2267 if (flags & (M_NOWAIT | M_NOVM))
2275 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2276 * with the keg locked. On NULL no lock is held.
2278 * The last pointer is used to seed the search. It is not required.
2281 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2291 * Don't wait on the first pass. This will skip limit tests
2292 * as well. We don't want to block if we can find a provider
2295 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2297 * Use the last slab allocated as a hint for where to start
2301 slab = keg_fetch_slab(last, zone, flags);
2307 * Loop until we have a slab incase of transient failures
2308 * while M_WAITOK is specified. I'm not sure this is 100%
2309 * required but we've done it for so long now.
2315 * Search the available kegs for slabs. Be careful to hold the
2316 * correct lock while calling into the keg layer.
2318 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2319 keg = klink->kl_keg;
2321 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2322 slab = keg_fetch_slab(keg, zone, flags);
2326 if (keg->uk_flags & UMA_ZFLAG_FULL)
2332 if (rflags & (M_NOWAIT | M_NOVM))
2336 * All kegs are full. XXX We can't atomically check all kegs
2337 * and sleep so just sleep for a short period and retry.
2339 if (full && !empty) {
2341 zone->uz_flags |= UMA_ZFLAG_FULL;
2343 zone_log_warning(zone);
2344 msleep(zone, zone->uz_lockptr, PVM,
2345 "zonelimit", hz/100);
2346 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2355 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2360 MPASS(keg == slab->us_keg);
2361 mtx_assert(&keg->uk_lock, MA_OWNED);
2363 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2364 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2365 item = slab->us_data + (keg->uk_rsize * freei);
2366 slab->us_freecount--;
2369 /* Move this slab to the full list */
2370 if (slab->us_freecount == 0) {
2371 LIST_REMOVE(slab, us_link);
2372 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2379 zone_import(uma_zone_t zone, void **bucket, int max, int flags)
2387 /* Try to keep the buckets totally full */
2388 for (i = 0; i < max; ) {
2389 if ((slab = zone->uz_slab(zone, keg, flags)) == NULL)
2392 while (slab->us_freecount && i < max) {
2393 bucket[i++] = slab_alloc_item(keg, slab);
2394 if (keg->uk_free <= keg->uk_reserve)
2397 /* Don't grab more than one slab at a time. */
2408 zone_alloc_bucket(uma_zone_t zone, void *udata, int flags)
2410 uma_bucket_t bucket;
2413 /* Don't wait for buckets, preserve caller's NOVM setting. */
2414 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
2418 max = MIN(bucket->ub_entries, zone->uz_count);
2419 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
2423 * Initialize the memory if necessary.
2425 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2428 for (i = 0; i < bucket->ub_cnt; i++)
2429 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2433 * If we couldn't initialize the whole bucket, put the
2434 * rest back onto the freelist.
2436 if (i != bucket->ub_cnt) {
2437 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
2438 bucket->ub_cnt - i);
2440 bzero(&bucket->ub_bucket[i],
2441 sizeof(void *) * (bucket->ub_cnt - i));
2448 if (bucket == NULL || bucket->ub_cnt == 0) {
2450 bucket_free(zone, bucket, udata);
2451 atomic_add_long(&zone->uz_fails, 1);
2459 * Allocates a single item from a zone.
2462 * zone The zone to alloc for.
2463 * udata The data to be passed to the constructor.
2464 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2467 * NULL if there is no memory and M_NOWAIT is set
2468 * An item if successful
2472 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2478 #ifdef UMA_DEBUG_ALLOC
2479 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2481 if (zone->uz_import(zone->uz_arg, &item, 1, flags) != 1)
2483 atomic_add_long(&zone->uz_allocs, 1);
2486 * We have to call both the zone's init (not the keg's init)
2487 * and the zone's ctor. This is because the item is going from
2488 * a keg slab directly to the user, and the user is expecting it
2489 * to be both zone-init'd as well as zone-ctor'd.
2491 if (zone->uz_init != NULL) {
2492 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2493 zone_free_item(zone, item, udata, SKIP_FINI);
2497 if (zone->uz_ctor != NULL) {
2498 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2499 zone_free_item(zone, item, udata, SKIP_DTOR);
2504 uma_dbg_alloc(zone, NULL, item);
2507 bzero(item, zone->uz_size);
2512 atomic_add_long(&zone->uz_fails, 1);
2518 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2521 uma_bucket_t bucket;
2524 #ifdef UMA_DEBUG_ALLOC_1
2525 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2527 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2530 /* uma_zfree(..., NULL) does nothing, to match free(9). */
2533 #ifdef DEBUG_MEMGUARD
2534 if (is_memguard_addr(item)) {
2535 if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor)
2536 zone->uz_dtor(item, zone->uz_size, udata);
2537 if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini)
2538 zone->uz_fini(item, zone->uz_size);
2539 memguard_free(item);
2544 if (zone->uz_flags & UMA_ZONE_MALLOC)
2545 uma_dbg_free(zone, udata, item);
2547 uma_dbg_free(zone, NULL, item);
2549 if (zone->uz_dtor != NULL)
2550 zone->uz_dtor(item, zone->uz_size, udata);
2553 * The race here is acceptable. If we miss it we'll just have to wait
2554 * a little longer for the limits to be reset.
2556 if (zone->uz_flags & UMA_ZFLAG_FULL)
2560 * If possible, free to the per-CPU cache. There are two
2561 * requirements for safe access to the per-CPU cache: (1) the thread
2562 * accessing the cache must not be preempted or yield during access,
2563 * and (2) the thread must not migrate CPUs without switching which
2564 * cache it accesses. We rely on a critical section to prevent
2565 * preemption and migration. We release the critical section in
2566 * order to acquire the zone mutex if we are unable to free to the
2567 * current cache; when we re-acquire the critical section, we must
2568 * detect and handle migration if it has occurred.
2573 cache = &zone->uz_cpu[cpu];
2577 * Try to free into the allocbucket first to give LIFO ordering
2578 * for cache-hot datastructures. Spill over into the freebucket
2579 * if necessary. Alloc will swap them if one runs dry.
2581 bucket = cache->uc_allocbucket;
2582 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
2583 bucket = cache->uc_freebucket;
2584 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2585 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2586 ("uma_zfree: Freeing to non free bucket index."));
2587 bucket->ub_bucket[bucket->ub_cnt] = item;
2595 * We must go back the zone, which requires acquiring the zone lock,
2596 * which in turn means we must release and re-acquire the critical
2597 * section. Since the critical section is released, we may be
2598 * preempted or migrate. As such, make sure not to maintain any
2599 * thread-local state specific to the cache from prior to releasing
2600 * the critical section.
2603 if (zone->uz_count == 0 || bucketdisable)
2609 cache = &zone->uz_cpu[cpu];
2612 * Since we have locked the zone we may as well send back our stats.
2614 atomic_add_long(&zone->uz_allocs, cache->uc_allocs);
2615 atomic_add_long(&zone->uz_frees, cache->uc_frees);
2616 cache->uc_allocs = 0;
2617 cache->uc_frees = 0;
2619 bucket = cache->uc_freebucket;
2620 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
2624 cache->uc_freebucket = NULL;
2626 /* Can we throw this on the zone full list? */
2627 if (bucket != NULL) {
2628 #ifdef UMA_DEBUG_ALLOC
2629 printf("uma_zfree: Putting old bucket on the free list.\n");
2631 /* ub_cnt is pointing to the last free item */
2632 KASSERT(bucket->ub_cnt != 0,
2633 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2634 LIST_INSERT_HEAD(&zone->uz_buckets, bucket, ub_link);
2637 /* We are no longer associated with this CPU. */
2640 /* And the zone.. */
2643 #ifdef UMA_DEBUG_ALLOC
2644 printf("uma_zfree: Allocating new free bucket.\n");
2646 bucket = bucket_alloc(zone, udata, M_NOWAIT);
2650 cache = &zone->uz_cpu[cpu];
2651 if (cache->uc_freebucket == NULL) {
2652 cache->uc_freebucket = bucket;
2656 * We lost the race, start over. We have to drop our
2657 * critical section to free the bucket.
2660 bucket_free(zone, bucket, udata);
2665 * If nothing else caught this, we'll just do an internal free.
2668 zone_free_item(zone, item, udata, SKIP_DTOR);
2674 slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
2678 mtx_assert(&keg->uk_lock, MA_OWNED);
2679 MPASS(keg == slab->us_keg);
2681 /* Do we need to remove from any lists? */
2682 if (slab->us_freecount+1 == keg->uk_ipers) {
2683 LIST_REMOVE(slab, us_link);
2684 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2685 } else if (slab->us_freecount == 0) {
2686 LIST_REMOVE(slab, us_link);
2687 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2690 /* Slab management. */
2691 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
2692 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
2693 slab->us_freecount++;
2695 /* Keg statistics. */
2700 zone_release(uma_zone_t zone, void **bucket, int cnt)
2710 keg = zone_first_keg(zone);
2712 for (i = 0; i < cnt; i++) {
2714 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2715 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
2716 if (zone->uz_flags & UMA_ZONE_HASH) {
2717 slab = hash_sfind(&keg->uk_hash, mem);
2719 mem += keg->uk_pgoff;
2720 slab = (uma_slab_t)mem;
2723 slab = vtoslab((vm_offset_t)item);
2724 if (slab->us_keg != keg) {
2730 slab_free_item(keg, slab, item);
2731 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2732 if (keg->uk_pages < keg->uk_maxpages) {
2733 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2738 * We can handle one more allocation. Since we're
2739 * clearing ZFLAG_FULL, wake up all procs blocked
2740 * on pages. This should be uncommon, so keeping this
2741 * simple for now (rather than adding count of blocked
2750 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2758 * Frees a single item to any zone.
2761 * zone The zone to free to
2762 * item The item we're freeing
2763 * udata User supplied data for the dtor
2764 * skip Skip dtors and finis
2767 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
2771 if (skip == SKIP_NONE) {
2772 if (zone->uz_flags & UMA_ZONE_MALLOC)
2773 uma_dbg_free(zone, udata, item);
2775 uma_dbg_free(zone, NULL, item);
2778 if (skip < SKIP_DTOR && zone->uz_dtor)
2779 zone->uz_dtor(item, zone->uz_size, udata);
2781 if (skip < SKIP_FINI && zone->uz_fini)
2782 zone->uz_fini(item, zone->uz_size);
2784 atomic_add_long(&zone->uz_frees, 1);
2785 zone->uz_release(zone->uz_arg, &item, 1);
2790 uma_zone_set_max(uma_zone_t zone, int nitems)
2794 keg = zone_first_keg(zone);
2798 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2799 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2800 keg->uk_maxpages += keg->uk_ppera;
2801 nitems = keg->uk_maxpages * keg->uk_ipers;
2809 uma_zone_get_max(uma_zone_t zone)
2814 keg = zone_first_keg(zone);
2818 nitems = keg->uk_maxpages * keg->uk_ipers;
2826 uma_zone_set_warning(uma_zone_t zone, const char *warning)
2830 zone->uz_warning = warning;
2836 uma_zone_get_cur(uma_zone_t zone)
2842 nitems = zone->uz_allocs - zone->uz_frees;
2845 * See the comment in sysctl_vm_zone_stats() regarding the
2846 * safety of accessing the per-cpu caches. With the zone lock
2847 * held, it is safe, but can potentially result in stale data.
2849 nitems += zone->uz_cpu[i].uc_allocs -
2850 zone->uz_cpu[i].uc_frees;
2854 return (nitems < 0 ? 0 : nitems);
2859 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2863 keg = zone_first_keg(zone);
2864 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2866 KASSERT(keg->uk_pages == 0,
2867 ("uma_zone_set_init on non-empty keg"));
2868 keg->uk_init = uminit;
2874 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2878 keg = zone_first_keg(zone);
2879 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2881 KASSERT(keg->uk_pages == 0,
2882 ("uma_zone_set_fini on non-empty keg"));
2883 keg->uk_fini = fini;
2889 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2893 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2894 ("uma_zone_set_zinit on non-empty keg"));
2895 zone->uz_init = zinit;
2901 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2905 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2906 ("uma_zone_set_zfini on non-empty keg"));
2907 zone->uz_fini = zfini;
2912 /* XXX uk_freef is not actually used with the zone locked */
2914 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2918 keg = zone_first_keg(zone);
2919 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
2921 keg->uk_freef = freef;
2926 /* XXX uk_allocf is not actually used with the zone locked */
2928 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2932 keg = zone_first_keg(zone);
2934 keg->uk_allocf = allocf;
2940 uma_zone_reserve(uma_zone_t zone, int items)
2944 keg = zone_first_keg(zone);
2948 keg->uk_reserve = items;
2956 uma_zone_reserve_kva(uma_zone_t zone, int count)
2962 keg = zone_first_keg(zone);
2965 pages = count / keg->uk_ipers;
2967 if (pages * keg->uk_ipers < count)
2970 #ifdef UMA_MD_SMALL_ALLOC
2971 if (keg->uk_ppera > 1) {
2975 kva = kva_alloc(pages * UMA_SLAB_SIZE);
2983 keg->uk_maxpages = pages;
2984 #ifdef UMA_MD_SMALL_ALLOC
2985 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
2987 keg->uk_allocf = noobj_alloc;
2989 keg->uk_flags |= UMA_ZONE_NOFREE;
2997 uma_prealloc(uma_zone_t zone, int items)
3003 keg = zone_first_keg(zone);
3007 slabs = items / keg->uk_ipers;
3008 if (slabs * keg->uk_ipers < items)
3011 slab = keg_alloc_slab(keg, zone, M_WAITOK);
3014 MPASS(slab->us_keg == keg);
3015 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
3023 uma_find_refcnt(uma_zone_t zone, void *item)
3025 uma_slabrefcnt_t slabref;
3031 slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
3032 slabref = (uma_slabrefcnt_t)slab;
3034 KASSERT(keg->uk_flags & UMA_ZONE_REFCNT,
3035 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3036 idx = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3037 refcnt = &slabref->us_refcnt[idx];
3046 printf("UMA: vm asked us to release pages!\n");
3049 zone_foreach(zone_drain);
3051 * Some slabs may have been freed but this zone will be visited early
3052 * we visit again so that we can free pages that are empty once other
3053 * zones are drained. We have to do the same for buckets.
3055 zone_drain(slabzone);
3056 zone_drain(slabrefzone);
3057 bucket_zone_drain();
3062 uma_zone_exhausted(uma_zone_t zone)
3067 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3073 uma_zone_exhausted_nolock(uma_zone_t zone)
3075 return (zone->uz_flags & UMA_ZFLAG_FULL);
3079 uma_large_malloc(int size, int wait)
3085 slab = zone_alloc_item(slabzone, NULL, wait);
3088 mem = page_alloc(NULL, size, &flags, wait);
3090 vsetslab((vm_offset_t)mem, slab);
3091 slab->us_data = mem;
3092 slab->us_flags = flags | UMA_SLAB_MALLOC;
3093 slab->us_size = size;
3095 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3102 uma_large_free(uma_slab_t slab)
3105 page_free(slab->us_data, slab->us_size, slab->us_flags);
3106 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3110 uma_print_stats(void)
3112 zone_foreach(uma_print_zone);
3116 slab_print(uma_slab_t slab)
3118 printf("slab: keg %p, data %p, freecount %d\n",
3119 slab->us_keg, slab->us_data, slab->us_freecount);
3123 cache_print(uma_cache_t cache)
3125 printf("alloc: %p(%d), free: %p(%d)\n",
3126 cache->uc_allocbucket,
3127 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3128 cache->uc_freebucket,
3129 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3133 uma_print_keg(uma_keg_t keg)
3137 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3138 "out %d free %d limit %d\n",
3139 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3140 keg->uk_ipers, keg->uk_ppera,
3141 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3142 (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3143 printf("Part slabs:\n");
3144 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3146 printf("Free slabs:\n");
3147 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3149 printf("Full slabs:\n");
3150 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3155 uma_print_zone(uma_zone_t zone)
3161 printf("zone: %s(%p) size %d flags %#x\n",
3162 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3163 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3164 uma_print_keg(kl->kl_keg);
3166 cache = &zone->uz_cpu[i];
3167 printf("CPU %d Cache:\n", i);
3174 * Generate statistics across both the zone and its per-cpu cache's. Return
3175 * desired statistics if the pointer is non-NULL for that statistic.
3177 * Note: does not update the zone statistics, as it can't safely clear the
3178 * per-CPU cache statistic.
3180 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3181 * safe from off-CPU; we should modify the caches to track this information
3182 * directly so that we don't have to.
3185 uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
3186 uint64_t *freesp, uint64_t *sleepsp)
3189 uint64_t allocs, frees, sleeps;
3192 allocs = frees = sleeps = 0;
3195 cache = &z->uz_cpu[cpu];
3196 if (cache->uc_allocbucket != NULL)
3197 cachefree += cache->uc_allocbucket->ub_cnt;
3198 if (cache->uc_freebucket != NULL)
3199 cachefree += cache->uc_freebucket->ub_cnt;
3200 allocs += cache->uc_allocs;
3201 frees += cache->uc_frees;
3203 allocs += z->uz_allocs;
3204 frees += z->uz_frees;
3205 sleeps += z->uz_sleeps;
3206 if (cachefreep != NULL)
3207 *cachefreep = cachefree;
3208 if (allocsp != NULL)
3212 if (sleepsp != NULL)
3218 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3226 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3227 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3230 mtx_unlock(&uma_mtx);
3231 return (sysctl_handle_int(oidp, &count, 0, req));
3235 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3237 struct uma_stream_header ush;
3238 struct uma_type_header uth;
3239 struct uma_percpu_stat ups;
3240 uma_bucket_t bucket;
3247 int count, error, i;
3249 error = sysctl_wire_old_buffer(req, 0);
3252 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3256 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3257 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3262 * Insert stream header.
3264 bzero(&ush, sizeof(ush));
3265 ush.ush_version = UMA_STREAM_VERSION;
3266 ush.ush_maxcpus = (mp_maxid + 1);
3267 ush.ush_count = count;
3268 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3270 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3271 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3272 bzero(&uth, sizeof(uth));
3274 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3275 uth.uth_align = kz->uk_align;
3276 uth.uth_size = kz->uk_size;
3277 uth.uth_rsize = kz->uk_rsize;
3278 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3280 uth.uth_maxpages += k->uk_maxpages;
3281 uth.uth_pages += k->uk_pages;
3282 uth.uth_keg_free += k->uk_free;
3283 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3288 * A zone is secondary is it is not the first entry
3289 * on the keg's zone list.
3291 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3292 (LIST_FIRST(&kz->uk_zones) != z))
3293 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3295 LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
3296 uth.uth_zone_free += bucket->ub_cnt;
3297 uth.uth_allocs = z->uz_allocs;
3298 uth.uth_frees = z->uz_frees;
3299 uth.uth_fails = z->uz_fails;
3300 uth.uth_sleeps = z->uz_sleeps;
3301 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
3303 * While it is not normally safe to access the cache
3304 * bucket pointers while not on the CPU that owns the
3305 * cache, we only allow the pointers to be exchanged
3306 * without the zone lock held, not invalidated, so
3307 * accept the possible race associated with bucket
3308 * exchange during monitoring.
3310 for (i = 0; i < (mp_maxid + 1); i++) {
3311 bzero(&ups, sizeof(ups));
3312 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3316 cache = &z->uz_cpu[i];
3317 if (cache->uc_allocbucket != NULL)
3318 ups.ups_cache_free +=
3319 cache->uc_allocbucket->ub_cnt;
3320 if (cache->uc_freebucket != NULL)
3321 ups.ups_cache_free +=
3322 cache->uc_freebucket->ub_cnt;
3323 ups.ups_allocs = cache->uc_allocs;
3324 ups.ups_frees = cache->uc_frees;
3326 (void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
3331 mtx_unlock(&uma_mtx);
3332 error = sbuf_finish(&sbuf);
3338 DB_SHOW_COMMAND(uma, db_show_uma)
3340 uint64_t allocs, frees, sleeps;
3341 uma_bucket_t bucket;
3346 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3347 "Requests", "Sleeps");
3348 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3349 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3350 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3351 allocs = z->uz_allocs;
3352 frees = z->uz_frees;
3353 sleeps = z->uz_sleeps;
3356 uma_zone_sumstat(z, &cachefree, &allocs,
3358 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3359 (LIST_FIRST(&kz->uk_zones) != z)))
3360 cachefree += kz->uk_free;
3361 LIST_FOREACH(bucket, &z->uz_buckets, ub_link)
3362 cachefree += bucket->ub_cnt;
3363 db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name,
3364 (uintmax_t)kz->uk_size,
3365 (intmax_t)(allocs - frees), cachefree,
3366 (uintmax_t)allocs, sleeps);