2 * Copyright (c) 2002-2005, 2009 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/kernel.h>
67 #include <sys/types.h>
68 #include <sys/queue.h>
69 #include <sys/malloc.h>
72 #include <sys/sysctl.h>
73 #include <sys/mutex.h>
77 #include <sys/vmmeter.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_param.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_kern.h>
85 #include <vm/vm_extern.h>
87 #include <vm/uma_int.h>
88 #include <vm/uma_dbg.h>
93 #include <vm/memguard.h>
97 * This is the zone and keg from which all zones are spawned. The idea is that
98 * even the zone & keg heads are allocated from the allocator, so we use the
99 * bss section to bootstrap us.
101 static struct uma_keg masterkeg;
102 static struct uma_zone masterzone_k;
103 static struct uma_zone masterzone_z;
104 static uma_zone_t kegs = &masterzone_k;
105 static uma_zone_t zones = &masterzone_z;
107 /* This is the zone from which all of uma_slab_t's are allocated. */
108 static uma_zone_t slabzone;
109 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
112 * The initial hash tables come out of this zone so they can be allocated
113 * prior to malloc coming up.
115 static uma_zone_t hashzone;
117 /* The boot-time adjusted value for cache line alignment. */
118 int uma_align_cache = 64 - 1;
120 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
123 * Are we allowed to allocate buckets?
125 static int bucketdisable = 1;
127 /* Linked list of all kegs in the system */
128 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
130 /* This mutex protects the keg list */
131 static struct mtx uma_mtx;
133 /* Linked list of boot time pages */
134 static LIST_HEAD(,uma_slab) uma_boot_pages =
135 LIST_HEAD_INITIALIZER(uma_boot_pages);
137 /* This mutex protects the boot time pages list */
138 static struct mtx uma_boot_pages_mtx;
140 /* Is the VM done starting up? */
141 static int booted = 0;
142 #define UMA_STARTUP 1
143 #define UMA_STARTUP2 2
145 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
146 static u_int uma_max_ipers;
147 static u_int uma_max_ipers_ref;
150 * This is the handle used to schedule events that need to happen
151 * outside of the allocation fast path.
153 static struct callout uma_callout;
154 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
157 * This structure is passed as the zone ctor arg so that I don't have to create
158 * a special allocation function just for zones.
160 struct uma_zctor_args {
172 struct uma_kctor_args {
181 struct uma_bucket_zone {
187 #define BUCKET_MAX 128
189 struct uma_bucket_zone bucket_zones[] = {
190 { NULL, "16 Bucket", 16 },
191 { NULL, "32 Bucket", 32 },
192 { NULL, "64 Bucket", 64 },
193 { NULL, "128 Bucket", 128 },
197 #define BUCKET_SHIFT 4
198 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
201 * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
202 * of approximately the right size.
204 static uint8_t bucket_size[BUCKET_ZONES];
207 * Flags and enumerations to be passed to internal functions.
209 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
211 #define ZFREE_STATFAIL 0x00000001 /* Update zone failure statistic. */
212 #define ZFREE_STATFREE 0x00000002 /* Update zone free statistic. */
216 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
217 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
218 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
219 static void page_free(void *, int, u_int8_t);
220 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
221 static void cache_drain(uma_zone_t);
222 static void bucket_drain(uma_zone_t, uma_bucket_t);
223 static void bucket_cache_drain(uma_zone_t zone);
224 static int keg_ctor(void *, int, void *, int);
225 static void keg_dtor(void *, int, void *);
226 static int zone_ctor(void *, int, void *, int);
227 static void zone_dtor(void *, int, void *);
228 static int zero_init(void *, int, int);
229 static void keg_small_init(uma_keg_t keg);
230 static void keg_large_init(uma_keg_t keg);
231 static void zone_foreach(void (*zfunc)(uma_zone_t));
232 static void zone_timeout(uma_zone_t zone);
233 static int hash_alloc(struct uma_hash *);
234 static int hash_expand(struct uma_hash *, struct uma_hash *);
235 static void hash_free(struct uma_hash *hash);
236 static void uma_timeout(void *);
237 static void uma_startup3(void);
238 static void *zone_alloc_item(uma_zone_t, void *, int);
239 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip,
241 static void bucket_enable(void);
242 static void bucket_init(void);
243 static uma_bucket_t bucket_alloc(int, int);
244 static void bucket_free(uma_bucket_t);
245 static void bucket_zone_drain(void);
246 static int zone_alloc_bucket(uma_zone_t zone, int flags);
247 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
248 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
249 static void *slab_alloc_item(uma_zone_t zone, uma_slab_t slab);
250 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
251 uma_fini fini, int align, u_int32_t flags);
252 static inline void zone_relock(uma_zone_t zone, uma_keg_t keg);
253 static inline void keg_relock(uma_keg_t keg, uma_zone_t zone);
255 void uma_print_zone(uma_zone_t);
256 void uma_print_stats(void);
257 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
258 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
260 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
262 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
263 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
265 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
266 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
269 * This routine checks to see whether or not it's safe to enable buckets.
275 if (cnt.v_free_count < cnt.v_free_min)
282 * Initialize bucket_zones, the array of zones of buckets of various sizes.
284 * For each zone, calculate the memory required for each bucket, consisting
285 * of the header and an array of pointers. Initialize bucket_size[] to point
286 * the range of appropriate bucket sizes at the zone.
291 struct uma_bucket_zone *ubz;
295 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
298 ubz = &bucket_zones[j];
299 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
300 size += sizeof(void *) * ubz->ubz_entries;
301 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
302 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
303 UMA_ZFLAG_INTERNAL | UMA_ZFLAG_BUCKET);
304 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
305 bucket_size[i >> BUCKET_SHIFT] = j;
310 * Given a desired number of entries for a bucket, return the zone from which
311 * to allocate the bucket.
313 static struct uma_bucket_zone *
314 bucket_zone_lookup(int entries)
318 idx = howmany(entries, 1 << BUCKET_SHIFT);
319 return (&bucket_zones[bucket_size[idx]]);
323 bucket_alloc(int entries, int bflags)
325 struct uma_bucket_zone *ubz;
329 * This is to stop us from allocating per cpu buckets while we're
330 * running out of vm.boot_pages. Otherwise, we would exhaust the
331 * boot pages. This also prevents us from allocating buckets in
332 * low memory situations.
337 ubz = bucket_zone_lookup(entries);
338 bucket = zone_alloc_item(ubz->ubz_zone, NULL, bflags);
341 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
344 bucket->ub_entries = ubz->ubz_entries;
351 bucket_free(uma_bucket_t bucket)
353 struct uma_bucket_zone *ubz;
355 ubz = bucket_zone_lookup(bucket->ub_entries);
356 zone_free_item(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
361 bucket_zone_drain(void)
363 struct uma_bucket_zone *ubz;
365 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
366 zone_drain(ubz->ubz_zone);
369 static inline uma_keg_t
370 zone_first_keg(uma_zone_t zone)
373 return (LIST_FIRST(&zone->uz_kegs)->kl_keg);
377 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
381 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
382 kegfn(klink->kl_keg);
386 * Routine called by timeout which is used to fire off some time interval
387 * based calculations. (stats, hash size, etc.)
396 uma_timeout(void *unused)
399 zone_foreach(zone_timeout);
401 /* Reschedule this event */
402 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
406 * Routine to perform timeout driven calculations. This expands the
407 * hashes and does per cpu statistics aggregation.
412 keg_timeout(uma_keg_t keg)
417 * Expand the keg hash table.
419 * This is done if the number of slabs is larger than the hash size.
420 * What I'm trying to do here is completely reduce collisions. This
421 * may be a little aggressive. Should I allow for two collisions max?
423 if (keg->uk_flags & UMA_ZONE_HASH &&
424 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
425 struct uma_hash newhash;
426 struct uma_hash oldhash;
430 * This is so involved because allocating and freeing
431 * while the keg lock is held will lead to deadlock.
432 * I have to do everything in stages and check for
435 newhash = keg->uk_hash;
437 ret = hash_alloc(&newhash);
440 if (hash_expand(&keg->uk_hash, &newhash)) {
441 oldhash = keg->uk_hash;
442 keg->uk_hash = newhash;
455 zone_timeout(uma_zone_t zone)
458 zone_foreach_keg(zone, &keg_timeout);
462 * Allocate and zero fill the next sized hash table from the appropriate
466 * hash A new hash structure with the old hash size in uh_hashsize
469 * 1 on sucess and 0 on failure.
472 hash_alloc(struct uma_hash *hash)
477 oldsize = hash->uh_hashsize;
479 /* We're just going to go to a power of two greater */
481 hash->uh_hashsize = oldsize * 2;
482 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
483 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
484 M_UMAHASH, M_NOWAIT);
486 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
487 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
489 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
491 if (hash->uh_slab_hash) {
492 bzero(hash->uh_slab_hash, alloc);
493 hash->uh_hashmask = hash->uh_hashsize - 1;
501 * Expands the hash table for HASH zones. This is done from zone_timeout
502 * to reduce collisions. This must not be done in the regular allocation
503 * path, otherwise, we can recurse on the vm while allocating pages.
506 * oldhash The hash you want to expand
507 * newhash The hash structure for the new table
515 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
521 if (!newhash->uh_slab_hash)
524 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
528 * I need to investigate hash algorithms for resizing without a
532 for (i = 0; i < oldhash->uh_hashsize; i++)
533 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
534 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
535 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
536 hval = UMA_HASH(newhash, slab->us_data);
537 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
545 * Free the hash bucket to the appropriate backing store.
548 * slab_hash The hash bucket we're freeing
549 * hashsize The number of entries in that hash bucket
555 hash_free(struct uma_hash *hash)
557 if (hash->uh_slab_hash == NULL)
559 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
560 zone_free_item(hashzone,
561 hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
563 free(hash->uh_slab_hash, M_UMAHASH);
567 * Frees all outstanding items in a bucket
570 * zone The zone to free to, must be unlocked.
571 * bucket The free/alloc bucket with items, cpu queue must be locked.
578 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
585 while (bucket->ub_cnt > 0) {
587 item = bucket->ub_bucket[bucket->ub_cnt];
589 bucket->ub_bucket[bucket->ub_cnt] = NULL;
590 KASSERT(item != NULL,
591 ("bucket_drain: botched ptr, item is NULL"));
593 zone_free_item(zone, item, NULL, SKIP_DTOR, 0);
598 * Drains the per cpu caches for a zone.
600 * NOTE: This may only be called while the zone is being turn down, and not
601 * during normal operation. This is necessary in order that we do not have
602 * to migrate CPUs to drain the per-CPU caches.
605 * zone The zone to drain, must be unlocked.
611 cache_drain(uma_zone_t zone)
617 * XXX: It is safe to not lock the per-CPU caches, because we're
618 * tearing down the zone anyway. I.e., there will be no further use
619 * of the caches at this point.
621 * XXX: It would good to be able to assert that the zone is being
622 * torn down to prevent improper use of cache_drain().
624 * XXX: We lock the zone before passing into bucket_cache_drain() as
625 * it is used elsewhere. Should the tear-down path be made special
626 * there in some form?
629 cache = &zone->uz_cpu[cpu];
630 bucket_drain(zone, cache->uc_allocbucket);
631 bucket_drain(zone, cache->uc_freebucket);
632 if (cache->uc_allocbucket != NULL)
633 bucket_free(cache->uc_allocbucket);
634 if (cache->uc_freebucket != NULL)
635 bucket_free(cache->uc_freebucket);
636 cache->uc_allocbucket = cache->uc_freebucket = NULL;
639 bucket_cache_drain(zone);
644 * Drain the cached buckets from a zone. Expects a locked zone on entry.
647 bucket_cache_drain(uma_zone_t zone)
652 * Drain the bucket queues and free the buckets, we just keep two per
655 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
656 LIST_REMOVE(bucket, ub_link);
658 bucket_drain(zone, bucket);
663 /* Now we do the free queue.. */
664 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
665 LIST_REMOVE(bucket, ub_link);
671 * Frees pages from a keg back to the system. This is done on demand from
672 * the pageout daemon.
677 keg_drain(uma_keg_t keg)
679 struct slabhead freeslabs = { 0 };
687 * We don't want to take pages from statically allocated kegs at this
690 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
694 printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
697 if (keg->uk_free == 0)
700 slab = LIST_FIRST(&keg->uk_free_slab);
702 n = LIST_NEXT(slab, us_link);
704 /* We have no where to free these to */
705 if (slab->us_flags & UMA_SLAB_BOOT) {
710 LIST_REMOVE(slab, us_link);
711 keg->uk_pages -= keg->uk_ppera;
712 keg->uk_free -= keg->uk_ipers;
714 if (keg->uk_flags & UMA_ZONE_HASH)
715 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
717 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
724 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
725 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
727 for (i = 0; i < keg->uk_ipers; i++)
729 slab->us_data + (keg->uk_rsize * i),
731 flags = slab->us_flags;
734 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
737 if (flags & UMA_SLAB_KMEM)
739 else if (flags & UMA_SLAB_KERNEL)
743 for (i = 0; i < keg->uk_ppera; i++)
744 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
747 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
748 zone_free_item(keg->uk_slabzone, slab, NULL,
749 SKIP_NONE, ZFREE_STATFREE);
751 printf("%s: Returning %d bytes.\n",
752 keg->uk_name, UMA_SLAB_SIZE * keg->uk_ppera);
754 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
759 zone_drain_wait(uma_zone_t zone, int waitok)
763 * Set draining to interlock with zone_dtor() so we can release our
764 * locks as we go. Only dtor() should do a WAITOK call since it
765 * is the only call that knows the structure will still be available
769 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
770 if (waitok == M_NOWAIT)
772 mtx_unlock(&uma_mtx);
773 msleep(zone, zone->uz_lock, PVM, "zonedrain", 1);
776 zone->uz_flags |= UMA_ZFLAG_DRAINING;
777 bucket_cache_drain(zone);
780 * The DRAINING flag protects us from being freed while
781 * we're running. Normally the uma_mtx would protect us but we
782 * must be able to release and acquire the right lock for each keg.
784 zone_foreach_keg(zone, &keg_drain);
786 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
793 zone_drain(uma_zone_t zone)
796 zone_drain_wait(zone, M_NOWAIT);
800 * Allocate a new slab for a keg. This does not insert the slab onto a list.
803 * wait Shall we wait?
806 * The slab that was allocated or NULL if there is no memory and the
807 * caller specified M_NOWAIT.
810 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
812 uma_slabrefcnt_t slabref;
819 mtx_assert(&keg->uk_lock, MA_OWNED);
823 printf("slab_zalloc: Allocating a new slab for %s\n", keg->uk_name);
825 allocf = keg->uk_allocf;
828 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
829 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
837 * This reproduces the old vm_zone behavior of zero filling pages the
838 * first time they are added to a zone.
840 * Malloced items are zeroed in uma_zalloc.
843 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
848 /* zone is passed for legacy reasons. */
849 mem = allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE, &flags, wait);
851 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
852 zone_free_item(keg->uk_slabzone, slab, NULL,
853 SKIP_NONE, ZFREE_STATFREE);
858 /* Point the slab into the allocated memory */
859 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
860 slab = (uma_slab_t )(mem + keg->uk_pgoff);
862 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
863 for (i = 0; i < keg->uk_ppera; i++)
864 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
868 slab->us_freecount = keg->uk_ipers;
869 slab->us_firstfree = 0;
870 slab->us_flags = flags;
872 if (keg->uk_flags & UMA_ZONE_REFCNT) {
873 slabref = (uma_slabrefcnt_t)slab;
874 for (i = 0; i < keg->uk_ipers; i++) {
875 slabref->us_freelist[i].us_refcnt = 0;
876 slabref->us_freelist[i].us_item = i+1;
879 for (i = 0; i < keg->uk_ipers; i++)
880 slab->us_freelist[i].us_item = i+1;
883 if (keg->uk_init != NULL) {
884 for (i = 0; i < keg->uk_ipers; i++)
885 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
886 keg->uk_size, wait) != 0)
888 if (i != keg->uk_ipers) {
889 if (keg->uk_fini != NULL) {
890 for (i--; i > -1; i--)
891 keg->uk_fini(slab->us_data +
895 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
898 if (flags & UMA_SLAB_KMEM)
900 else if (flags & UMA_SLAB_KERNEL)
904 for (i = 0; i < keg->uk_ppera; i++)
905 vsetobj((vm_offset_t)mem +
906 (i * PAGE_SIZE), obj);
908 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
909 zone_free_item(keg->uk_slabzone, slab,
910 NULL, SKIP_NONE, ZFREE_STATFREE);
911 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
919 if (keg->uk_flags & UMA_ZONE_HASH)
920 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
922 keg->uk_pages += keg->uk_ppera;
923 keg->uk_free += keg->uk_ipers;
929 * This function is intended to be used early on in place of page_alloc() so
930 * that we may use the boot time page cache to satisfy allocations before
934 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
938 int pages, check_pages;
940 keg = zone_first_keg(zone);
941 pages = howmany(bytes, PAGE_SIZE);
942 check_pages = pages - 1;
943 KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
946 * Check our small startup cache to see if it has pages remaining.
948 mtx_lock(&uma_boot_pages_mtx);
950 /* First check if we have enough room. */
951 tmps = LIST_FIRST(&uma_boot_pages);
952 while (tmps != NULL && check_pages-- > 0)
953 tmps = LIST_NEXT(tmps, us_link);
956 * It's ok to lose tmps references. The last one will
957 * have tmps->us_data pointing to the start address of
958 * "pages" contiguous pages of memory.
960 while (pages-- > 0) {
961 tmps = LIST_FIRST(&uma_boot_pages);
962 LIST_REMOVE(tmps, us_link);
964 mtx_unlock(&uma_boot_pages_mtx);
965 *pflag = tmps->us_flags;
966 return (tmps->us_data);
968 mtx_unlock(&uma_boot_pages_mtx);
969 if (booted < UMA_STARTUP2)
970 panic("UMA: Increase vm.boot_pages");
972 * Now that we've booted reset these users to their real allocator.
974 #ifdef UMA_MD_SMALL_ALLOC
975 keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
977 keg->uk_allocf = page_alloc;
979 return keg->uk_allocf(zone, bytes, pflag, wait);
983 * Allocates a number of pages from the system
986 * bytes The number of bytes requested
987 * wait Shall we wait?
990 * A pointer to the alloced memory or possibly
991 * NULL if M_NOWAIT is set.
994 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
996 void *p; /* Returned page */
998 *pflag = UMA_SLAB_KMEM;
999 p = (void *) kmem_malloc(kmem_map, bytes, wait);
1005 * Allocates a number of pages from within an object
1008 * bytes The number of bytes requested
1009 * wait Shall we wait?
1012 * A pointer to the alloced memory or possibly
1013 * NULL if M_NOWAIT is set.
1016 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
1019 vm_offset_t retkva, zkva;
1021 int pages, startpages;
1024 keg = zone_first_keg(zone);
1025 object = keg->uk_obj;
1029 * This looks a little weird since we're getting one page at a time.
1031 VM_OBJECT_LOCK(object);
1032 p = TAILQ_LAST(&object->memq, pglist);
1033 pages = p != NULL ? p->pindex + 1 : 0;
1035 zkva = keg->uk_kva + pages * PAGE_SIZE;
1036 for (; bytes > 0; bytes -= PAGE_SIZE) {
1037 p = vm_page_alloc(object, pages,
1038 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
1040 if (pages != startpages)
1041 pmap_qremove(retkva, pages - startpages);
1042 while (pages != startpages) {
1044 p = TAILQ_LAST(&object->memq, pglist);
1045 vm_page_unwire(p, 0);
1051 pmap_qenter(zkva, &p, 1);
1058 VM_OBJECT_UNLOCK(object);
1059 *flags = UMA_SLAB_PRIV;
1061 return ((void *)retkva);
1065 * Frees a number of pages to the system
1068 * mem A pointer to the memory to be freed
1069 * size The size of the memory being freed
1070 * flags The original p->us_flags field
1076 page_free(void *mem, int size, u_int8_t flags)
1080 if (flags & UMA_SLAB_KMEM)
1082 else if (flags & UMA_SLAB_KERNEL)
1085 panic("UMA: page_free used with invalid flags %d", flags);
1087 kmem_free(map, (vm_offset_t)mem, size);
1091 * Zero fill initializer
1093 * Arguments/Returns follow uma_init specifications
1096 zero_init(void *mem, int size, int flags)
1103 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1106 * keg The zone we should initialize
1112 keg_small_init(uma_keg_t keg)
1119 KASSERT(keg != NULL, ("Keg is null in keg_small_init"));
1120 rsize = keg->uk_size;
1122 if (rsize < UMA_SMALLEST_UNIT)
1123 rsize = UMA_SMALLEST_UNIT;
1124 if (rsize & keg->uk_align)
1125 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1127 keg->uk_rsize = rsize;
1130 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1131 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1132 shsize = sizeof(struct uma_slab_refcnt);
1134 rsize += UMA_FRITM_SZ; /* Account for linkage */
1135 shsize = sizeof(struct uma_slab);
1138 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1139 KASSERT(keg->uk_ipers != 0, ("keg_small_init: ipers is 0"));
1140 memused = keg->uk_ipers * rsize + shsize;
1141 wastedspace = UMA_SLAB_SIZE - memused;
1144 * We can't do OFFPAGE if we're internal or if we've been
1145 * asked to not go to the VM for buckets. If we do this we
1146 * may end up going to the VM (kmem_map) for slabs which we
1147 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1148 * result of UMA_ZONE_VM, which clearly forbids it.
1150 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1151 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1154 if ((wastedspace >= UMA_MAX_WASTE) &&
1155 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1156 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1157 KASSERT(keg->uk_ipers <= 255,
1158 ("keg_small_init: keg->uk_ipers too high!"));
1160 printf("UMA decided we need offpage slab headers for "
1161 "keg: %s, calculated wastedspace = %d, "
1162 "maximum wasted space allowed = %d, "
1163 "calculated ipers = %d, "
1164 "new wasted space = %d\n", keg->uk_name, wastedspace,
1165 UMA_MAX_WASTE, keg->uk_ipers,
1166 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1168 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1169 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1170 keg->uk_flags |= UMA_ZONE_HASH;
1175 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1176 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1180 * keg The keg we should initialize
1186 keg_large_init(uma_keg_t keg)
1190 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1191 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1192 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1194 pages = keg->uk_size / UMA_SLAB_SIZE;
1196 /* Account for remainder */
1197 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1200 keg->uk_ppera = pages;
1202 keg->uk_rsize = keg->uk_size;
1204 /* We can't do OFFPAGE if we're internal, bail out here. */
1205 if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1208 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1209 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1210 keg->uk_flags |= UMA_ZONE_HASH;
1214 keg_cachespread_init(uma_keg_t keg)
1221 alignsize = keg->uk_align + 1;
1222 rsize = keg->uk_size;
1224 * We want one item to start on every align boundary in a page. To
1225 * do this we will span pages. We will also extend the item by the
1226 * size of align if it is an even multiple of align. Otherwise, it
1227 * would fall on the same boundary every time.
1229 if (rsize & keg->uk_align)
1230 rsize = (rsize & ~keg->uk_align) + alignsize;
1231 if ((rsize & alignsize) == 0)
1233 trailer = rsize - keg->uk_size;
1234 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1235 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1236 keg->uk_rsize = rsize;
1237 keg->uk_ppera = pages;
1238 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1239 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1240 KASSERT(keg->uk_ipers <= uma_max_ipers,
1241 ("keg_small_init: keg->uk_ipers too high(%d) increase max_ipers",
1246 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1247 * the keg onto the global keg list.
1249 * Arguments/Returns follow uma_ctor specifications
1250 * udata Actually uma_kctor_args
1253 keg_ctor(void *mem, int size, void *udata, int flags)
1255 struct uma_kctor_args *arg = udata;
1256 uma_keg_t keg = mem;
1260 keg->uk_size = arg->size;
1261 keg->uk_init = arg->uminit;
1262 keg->uk_fini = arg->fini;
1263 keg->uk_align = arg->align;
1266 keg->uk_flags = arg->flags;
1267 keg->uk_allocf = page_alloc;
1268 keg->uk_freef = page_free;
1269 keg->uk_recurse = 0;
1270 keg->uk_slabzone = NULL;
1273 * The master zone is passed to us at keg-creation time.
1276 keg->uk_name = zone->uz_name;
1278 if (arg->flags & UMA_ZONE_VM)
1279 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1281 if (arg->flags & UMA_ZONE_ZINIT)
1282 keg->uk_init = zero_init;
1284 if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1285 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1288 * The +UMA_FRITM_SZ added to uk_size is to account for the
1289 * linkage that is added to the size in keg_small_init(). If
1290 * we don't account for this here then we may end up in
1291 * keg_small_init() with a calculated 'ipers' of 0.
1293 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1294 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1295 keg_cachespread_init(keg);
1296 else if ((keg->uk_size+UMA_FRITMREF_SZ) >
1297 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1298 keg_large_init(keg);
1300 keg_small_init(keg);
1302 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1303 keg_cachespread_init(keg);
1304 else if ((keg->uk_size+UMA_FRITM_SZ) >
1305 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1306 keg_large_init(keg);
1308 keg_small_init(keg);
1311 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1312 if (keg->uk_flags & UMA_ZONE_REFCNT)
1313 keg->uk_slabzone = slabrefzone;
1315 keg->uk_slabzone = slabzone;
1319 * If we haven't booted yet we need allocations to go through the
1320 * startup cache until the vm is ready.
1322 if (keg->uk_ppera == 1) {
1323 #ifdef UMA_MD_SMALL_ALLOC
1324 keg->uk_allocf = uma_small_alloc;
1325 keg->uk_freef = uma_small_free;
1327 if (booted < UMA_STARTUP)
1328 keg->uk_allocf = startup_alloc;
1330 if (booted < UMA_STARTUP2)
1331 keg->uk_allocf = startup_alloc;
1333 } else if (booted < UMA_STARTUP2 &&
1334 (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1335 keg->uk_allocf = startup_alloc;
1338 * Initialize keg's lock (shared among zones).
1340 if (arg->flags & UMA_ZONE_MTXCLASS)
1341 KEG_LOCK_INIT(keg, 1);
1343 KEG_LOCK_INIT(keg, 0);
1346 * If we're putting the slab header in the actual page we need to
1347 * figure out where in each page it goes. This calculates a right
1348 * justified offset into the memory on an ALIGN_PTR boundary.
1350 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1353 /* Size of the slab struct and free list */
1354 if (keg->uk_flags & UMA_ZONE_REFCNT)
1355 totsize = sizeof(struct uma_slab_refcnt) +
1356 keg->uk_ipers * UMA_FRITMREF_SZ;
1358 totsize = sizeof(struct uma_slab) +
1359 keg->uk_ipers * UMA_FRITM_SZ;
1361 if (totsize & UMA_ALIGN_PTR)
1362 totsize = (totsize & ~UMA_ALIGN_PTR) +
1363 (UMA_ALIGN_PTR + 1);
1364 keg->uk_pgoff = (UMA_SLAB_SIZE * keg->uk_ppera) - totsize;
1366 if (keg->uk_flags & UMA_ZONE_REFCNT)
1367 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1368 + keg->uk_ipers * UMA_FRITMREF_SZ;
1370 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1371 + keg->uk_ipers * UMA_FRITM_SZ;
1374 * The only way the following is possible is if with our
1375 * UMA_ALIGN_PTR adjustments we are now bigger than
1376 * UMA_SLAB_SIZE. I haven't checked whether this is
1377 * mathematically possible for all cases, so we make
1380 if (totsize > UMA_SLAB_SIZE * keg->uk_ppera) {
1381 printf("zone %s ipers %d rsize %d size %d\n",
1382 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1384 panic("UMA slab won't fit.");
1388 if (keg->uk_flags & UMA_ZONE_HASH)
1389 hash_alloc(&keg->uk_hash);
1392 printf("UMA: %s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
1393 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1394 keg->uk_ipers, keg->uk_ppera,
1395 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1398 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1401 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1402 mtx_unlock(&uma_mtx);
1407 * Zone header ctor. This initializes all fields, locks, etc.
1409 * Arguments/Returns follow uma_ctor specifications
1410 * udata Actually uma_zctor_args
1413 zone_ctor(void *mem, int size, void *udata, int flags)
1415 struct uma_zctor_args *arg = udata;
1416 uma_zone_t zone = mem;
1421 zone->uz_name = arg->name;
1422 zone->uz_ctor = arg->ctor;
1423 zone->uz_dtor = arg->dtor;
1424 zone->uz_slab = zone_fetch_slab;
1425 zone->uz_init = NULL;
1426 zone->uz_fini = NULL;
1427 zone->uz_allocs = 0;
1430 zone->uz_sleeps = 0;
1431 zone->uz_fills = zone->uz_count = 0;
1435 if (arg->flags & UMA_ZONE_SECONDARY) {
1436 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1437 zone->uz_init = arg->uminit;
1438 zone->uz_fini = arg->fini;
1439 zone->uz_lock = &keg->uk_lock;
1440 zone->uz_flags |= UMA_ZONE_SECONDARY;
1443 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1444 if (LIST_NEXT(z, uz_link) == NULL) {
1445 LIST_INSERT_AFTER(z, zone, uz_link);
1450 mtx_unlock(&uma_mtx);
1451 } else if (keg == NULL) {
1452 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1453 arg->align, arg->flags)) == NULL)
1456 struct uma_kctor_args karg;
1459 /* We should only be here from uma_startup() */
1460 karg.size = arg->size;
1461 karg.uminit = arg->uminit;
1462 karg.fini = arg->fini;
1463 karg.align = arg->align;
1464 karg.flags = arg->flags;
1466 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1472 * Link in the first keg.
1474 zone->uz_klink.kl_keg = keg;
1475 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1476 zone->uz_lock = &keg->uk_lock;
1477 zone->uz_size = keg->uk_size;
1478 zone->uz_flags |= (keg->uk_flags &
1479 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1482 * Some internal zones don't have room allocated for the per cpu
1483 * caches. If we're internal, bail out here.
1485 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1486 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1487 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1491 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1492 zone->uz_count = BUCKET_MAX;
1493 else if (keg->uk_ipers <= BUCKET_MAX)
1494 zone->uz_count = keg->uk_ipers;
1496 zone->uz_count = BUCKET_MAX;
1501 * Keg header dtor. This frees all data, destroys locks, frees the hash
1502 * table and removes the keg from the global list.
1504 * Arguments/Returns follow uma_dtor specifications
1508 keg_dtor(void *arg, int size, void *udata)
1512 keg = (uma_keg_t)arg;
1514 if (keg->uk_free != 0) {
1515 printf("Freed UMA keg was not empty (%d items). "
1516 " Lost %d pages of memory.\n",
1517 keg->uk_free, keg->uk_pages);
1521 hash_free(&keg->uk_hash);
1529 * Arguments/Returns follow uma_dtor specifications
1533 zone_dtor(void *arg, int size, void *udata)
1539 zone = (uma_zone_t)arg;
1540 keg = zone_first_keg(zone);
1542 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1546 LIST_REMOVE(zone, uz_link);
1547 mtx_unlock(&uma_mtx);
1549 * XXX there are some races here where
1550 * the zone can be drained but zone lock
1551 * released and then refilled before we
1552 * remove it... we dont care for now
1554 zone_drain_wait(zone, M_WAITOK);
1556 * Unlink all of our kegs.
1558 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1559 klink->kl_keg = NULL;
1560 LIST_REMOVE(klink, kl_link);
1561 if (klink == &zone->uz_klink)
1563 free(klink, M_TEMP);
1566 * We only destroy kegs from non secondary zones.
1568 if ((zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1570 LIST_REMOVE(keg, uk_link);
1571 mtx_unlock(&uma_mtx);
1572 zone_free_item(kegs, keg, NULL, SKIP_NONE,
1578 * Traverses every zone in the system and calls a callback
1581 * zfunc A pointer to a function which accepts a zone
1588 zone_foreach(void (*zfunc)(uma_zone_t))
1594 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1595 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1598 mtx_unlock(&uma_mtx);
1601 /* Public functions */
1604 uma_startup(void *bootmem, int boot_pages)
1606 struct uma_zctor_args args;
1609 u_int objsize, totsize, wsize;
1613 printf("Creating uma keg headers zone and keg.\n");
1615 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1618 * Figure out the maximum number of items-per-slab we'll have if
1619 * we're using the OFFPAGE slab header to track free items, given
1620 * all possible object sizes and the maximum desired wastage
1623 * We iterate until we find an object size for
1624 * which the calculated wastage in keg_small_init() will be
1625 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1626 * is an overall increasing see-saw function, we find the smallest
1627 * objsize such that the wastage is always acceptable for objects
1628 * with that objsize or smaller. Since a smaller objsize always
1629 * generates a larger possible uma_max_ipers, we use this computed
1630 * objsize to calculate the largest ipers possible. Since the
1631 * ipers calculated for OFFPAGE slab headers is always larger than
1632 * the ipers initially calculated in keg_small_init(), we use
1633 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1634 * obtain the maximum ipers possible for offpage slab headers.
1636 * It should be noted that ipers versus objsize is an inversly
1637 * proportional function which drops off rather quickly so as
1638 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1639 * falls into the portion of the inverse relation AFTER the steep
1640 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1642 * Note that we have 8-bits (1 byte) to use as a freelist index
1643 * inside the actual slab header itself and this is enough to
1644 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1645 * object with offpage slab header would have ipers =
1646 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1647 * 1 greater than what our byte-integer freelist index can
1648 * accomodate, but we know that this situation never occurs as
1649 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1650 * that we need to go to offpage slab headers. Or, if we do,
1651 * then we trap that condition below and panic in the INVARIANTS case.
1653 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1655 objsize = UMA_SMALLEST_UNIT;
1656 while (totsize >= wsize) {
1657 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1658 (objsize + UMA_FRITM_SZ);
1659 totsize *= (UMA_FRITM_SZ + objsize);
1662 if (objsize > UMA_SMALLEST_UNIT)
1664 uma_max_ipers = MAX(UMA_SLAB_SIZE / objsize, 64);
1666 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1668 objsize = UMA_SMALLEST_UNIT;
1669 while (totsize >= wsize) {
1670 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1671 (objsize + UMA_FRITMREF_SZ);
1672 totsize *= (UMA_FRITMREF_SZ + objsize);
1675 if (objsize > UMA_SMALLEST_UNIT)
1677 uma_max_ipers_ref = MAX(UMA_SLAB_SIZE / objsize, 64);
1679 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1680 ("uma_startup: calculated uma_max_ipers values too large!"));
1683 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1684 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1688 /* "manually" create the initial zone */
1689 args.name = "UMA Kegs";
1690 args.size = sizeof(struct uma_keg);
1691 args.ctor = keg_ctor;
1692 args.dtor = keg_dtor;
1693 args.uminit = zero_init;
1695 args.keg = &masterkeg;
1696 args.align = 32 - 1;
1697 args.flags = UMA_ZFLAG_INTERNAL;
1698 /* The initial zone has no Per cpu queues so it's smaller */
1699 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1702 printf("Filling boot free list.\n");
1704 for (i = 0; i < boot_pages; i++) {
1705 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1706 slab->us_data = (u_int8_t *)slab;
1707 slab->us_flags = UMA_SLAB_BOOT;
1708 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1710 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1713 printf("Creating uma zone headers zone and keg.\n");
1715 args.name = "UMA Zones";
1716 args.size = sizeof(struct uma_zone) +
1717 (sizeof(struct uma_cache) * (mp_maxid + 1));
1718 args.ctor = zone_ctor;
1719 args.dtor = zone_dtor;
1720 args.uminit = zero_init;
1723 args.align = 32 - 1;
1724 args.flags = UMA_ZFLAG_INTERNAL;
1725 /* The initial zone has no Per cpu queues so it's smaller */
1726 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1729 printf("Initializing pcpu cache locks.\n");
1732 printf("Creating slab and hash zones.\n");
1736 * This is the max number of free list items we'll have with
1739 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1740 slabsize += sizeof(struct uma_slab);
1742 /* Now make a zone for slab headers */
1743 slabzone = uma_zcreate("UMA Slabs",
1745 NULL, NULL, NULL, NULL,
1746 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1749 * We also create a zone for the bigger slabs with reference
1750 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1752 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1753 slabsize += sizeof(struct uma_slab_refcnt);
1754 slabrefzone = uma_zcreate("UMA RCntSlabs",
1756 NULL, NULL, NULL, NULL,
1758 UMA_ZFLAG_INTERNAL);
1760 hashzone = uma_zcreate("UMA Hash",
1761 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1762 NULL, NULL, NULL, NULL,
1763 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1767 booted = UMA_STARTUP;
1770 printf("UMA startup complete.\n");
1778 booted = UMA_STARTUP2;
1781 printf("UMA startup2 complete.\n");
1786 * Initialize our callout handle
1794 printf("Starting callout.\n");
1796 callout_init(&uma_callout, CALLOUT_MPSAFE);
1797 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1799 printf("UMA startup3 complete.\n");
1804 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1805 int align, u_int32_t flags)
1807 struct uma_kctor_args args;
1810 args.uminit = uminit;
1812 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1815 return (zone_alloc_item(kegs, &args, M_WAITOK));
1820 uma_set_align(int align)
1823 if (align != UMA_ALIGN_CACHE)
1824 uma_align_cache = align;
1829 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1830 uma_init uminit, uma_fini fini, int align, u_int32_t flags)
1833 struct uma_zctor_args args;
1835 /* This stuff is essential for the zone ctor */
1840 args.uminit = uminit;
1846 return (zone_alloc_item(zones, &args, M_WAITOK));
1851 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1852 uma_init zinit, uma_fini zfini, uma_zone_t master)
1854 struct uma_zctor_args args;
1857 keg = zone_first_keg(master);
1859 args.size = keg->uk_size;
1862 args.uminit = zinit;
1864 args.align = keg->uk_align;
1865 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1868 /* XXX Attaches only one keg of potentially many. */
1869 return (zone_alloc_item(zones, &args, M_WAITOK));
1873 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1877 mtx_lock_flags(b->uz_lock, MTX_DUPOK);
1880 mtx_lock_flags(a->uz_lock, MTX_DUPOK);
1885 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1893 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1900 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1902 zone_lock_pair(zone, master);
1904 * zone must use vtoslab() to resolve objects and must already be
1907 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1908 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1913 * The new master must also use vtoslab().
1915 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1920 * Both must either be refcnt, or not be refcnt.
1922 if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1923 (master->uz_flags & UMA_ZONE_REFCNT)) {
1928 * The underlying object must be the same size. rsize
1931 if (master->uz_size != zone->uz_size) {
1936 * Put it at the end of the list.
1938 klink->kl_keg = zone_first_keg(master);
1939 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1940 if (LIST_NEXT(kl, kl_link) == NULL) {
1941 LIST_INSERT_AFTER(kl, klink, kl_link);
1946 zone->uz_flags |= UMA_ZFLAG_MULTI;
1947 zone->uz_slab = zone_fetch_slab_multi;
1950 zone_unlock_pair(zone, master);
1952 free(klink, M_TEMP);
1960 uma_zdestroy(uma_zone_t zone)
1963 zone_free_item(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1968 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1972 uma_bucket_t bucket;
1975 /* This is the fast path allocation */
1976 #ifdef UMA_DEBUG_ALLOC_1
1977 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1979 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1980 zone->uz_name, flags);
1982 if (flags & M_WAITOK) {
1983 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1984 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1986 #ifdef DEBUG_MEMGUARD
1987 if (memguard_cmp_zone(zone)) {
1988 item = memguard_alloc(zone->uz_size, flags);
1991 * Avoid conflict with the use-after-free
1992 * protecting infrastructure from INVARIANTS.
1994 if (zone->uz_init != NULL &&
1995 zone->uz_init != mtrash_init &&
1996 zone->uz_init(item, zone->uz_size, flags) != 0)
1998 if (zone->uz_ctor != NULL &&
1999 zone->uz_ctor != mtrash_ctor &&
2000 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2001 zone->uz_fini(item, zone->uz_size);
2006 /* This is unfortunate but should not be fatal. */
2010 * If possible, allocate from the per-CPU cache. There are two
2011 * requirements for safe access to the per-CPU cache: (1) the thread
2012 * accessing the cache must not be preempted or yield during access,
2013 * and (2) the thread must not migrate CPUs without switching which
2014 * cache it accesses. We rely on a critical section to prevent
2015 * preemption and migration. We release the critical section in
2016 * order to acquire the zone mutex if we are unable to allocate from
2017 * the current cache; when we re-acquire the critical section, we
2018 * must detect and handle migration if it has occurred.
2023 cache = &zone->uz_cpu[cpu];
2026 bucket = cache->uc_allocbucket;
2029 if (bucket->ub_cnt > 0) {
2031 item = bucket->ub_bucket[bucket->ub_cnt];
2033 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2035 KASSERT(item != NULL,
2036 ("uma_zalloc: Bucket pointer mangled."));
2041 uma_dbg_alloc(zone, NULL, item);
2044 if (zone->uz_ctor != NULL) {
2045 if (zone->uz_ctor(item, zone->uz_size,
2046 udata, flags) != 0) {
2047 zone_free_item(zone, item, udata,
2048 SKIP_DTOR, ZFREE_STATFAIL |
2054 bzero(item, zone->uz_size);
2056 } else if (cache->uc_freebucket) {
2058 * We have run out of items in our allocbucket.
2059 * See if we can switch with our free bucket.
2061 if (cache->uc_freebucket->ub_cnt > 0) {
2062 #ifdef UMA_DEBUG_ALLOC
2063 printf("uma_zalloc: Swapping empty with"
2066 bucket = cache->uc_freebucket;
2067 cache->uc_freebucket = cache->uc_allocbucket;
2068 cache->uc_allocbucket = bucket;
2075 * Attempt to retrieve the item from the per-CPU cache has failed, so
2076 * we must go back to the zone. This requires the zone lock, so we
2077 * must drop the critical section, then re-acquire it when we go back
2078 * to the cache. Since the critical section is released, we may be
2079 * preempted or migrate. As such, make sure not to maintain any
2080 * thread-local state specific to the cache from prior to releasing
2081 * the critical section.
2087 cache = &zone->uz_cpu[cpu];
2088 bucket = cache->uc_allocbucket;
2089 if (bucket != NULL) {
2090 if (bucket->ub_cnt > 0) {
2094 bucket = cache->uc_freebucket;
2095 if (bucket != NULL && bucket->ub_cnt > 0) {
2101 /* Since we have locked the zone we may as well send back our stats */
2102 zone->uz_allocs += cache->uc_allocs;
2103 cache->uc_allocs = 0;
2104 zone->uz_frees += cache->uc_frees;
2105 cache->uc_frees = 0;
2107 /* Our old one is now a free bucket */
2108 if (cache->uc_allocbucket) {
2109 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
2110 ("uma_zalloc_arg: Freeing a non free bucket."));
2111 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2112 cache->uc_allocbucket, ub_link);
2113 cache->uc_allocbucket = NULL;
2116 /* Check the free list for a new alloc bucket */
2117 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
2118 KASSERT(bucket->ub_cnt != 0,
2119 ("uma_zalloc_arg: Returning an empty bucket."));
2121 LIST_REMOVE(bucket, ub_link);
2122 cache->uc_allocbucket = bucket;
2126 /* We are no longer associated with this CPU. */
2129 /* Bump up our uz_count so we get here less */
2130 if (zone->uz_count < BUCKET_MAX)
2134 * Now lets just fill a bucket and put it on the free list. If that
2135 * works we'll restart the allocation from the begining.
2137 if (zone_alloc_bucket(zone, flags)) {
2139 goto zalloc_restart;
2143 * We may not be able to get a bucket so return an actual item.
2146 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2149 item = zone_alloc_item(zone, udata, flags);
2154 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2158 mtx_assert(&keg->uk_lock, MA_OWNED);
2163 * Find a slab with some space. Prefer slabs that are partially
2164 * used over those that are totally full. This helps to reduce
2167 if (keg->uk_free != 0) {
2168 if (!LIST_EMPTY(&keg->uk_part_slab)) {
2169 slab = LIST_FIRST(&keg->uk_part_slab);
2171 slab = LIST_FIRST(&keg->uk_free_slab);
2172 LIST_REMOVE(slab, us_link);
2173 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2176 MPASS(slab->us_keg == keg);
2181 * M_NOVM means don't ask at all!
2186 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2187 keg->uk_flags |= UMA_ZFLAG_FULL;
2189 * If this is not a multi-zone, set the FULL bit.
2190 * Otherwise slab_multi() takes care of it.
2192 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0)
2193 zone->uz_flags |= UMA_ZFLAG_FULL;
2194 if (flags & M_NOWAIT)
2196 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2200 slab = keg_alloc_slab(keg, zone, flags);
2203 * If we got a slab here it's safe to mark it partially used
2204 * and return. We assume that the caller is going to remove
2205 * at least one item.
2208 MPASS(slab->us_keg == keg);
2209 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2213 * We might not have been able to get a slab but another cpu
2214 * could have while we were unlocked. Check again before we
2223 zone_relock(uma_zone_t zone, uma_keg_t keg)
2225 if (zone->uz_lock != &keg->uk_lock) {
2232 keg_relock(uma_keg_t keg, uma_zone_t zone)
2234 if (zone->uz_lock != &keg->uk_lock) {
2241 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2246 keg = zone_first_keg(zone);
2248 * This is to prevent us from recursively trying to allocate
2249 * buckets. The problem is that if an allocation forces us to
2250 * grab a new bucket we will call page_alloc, which will go off
2251 * and cause the vm to allocate vm_map_entries. If we need new
2252 * buckets there too we will recurse in kmem_alloc and bad
2253 * things happen. So instead we return a NULL bucket, and make
2254 * the code that allocates buckets smart enough to deal with it
2256 if (keg->uk_flags & UMA_ZFLAG_BUCKET && keg->uk_recurse != 0)
2260 slab = keg_fetch_slab(keg, zone, flags);
2263 if (flags & (M_NOWAIT | M_NOVM))
2270 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2271 * with the keg locked. Caller must call zone_relock() afterwards if the
2272 * zone lock is required. On NULL the zone lock is held.
2274 * The last pointer is used to seed the search. It is not required.
2277 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2287 * Don't wait on the first pass. This will skip limit tests
2288 * as well. We don't want to block if we can find a provider
2291 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2293 * Use the last slab allocated as a hint for where to start
2297 slab = keg_fetch_slab(last, zone, flags);
2300 zone_relock(zone, last);
2304 * Loop until we have a slab incase of transient failures
2305 * while M_WAITOK is specified. I'm not sure this is 100%
2306 * required but we've done it for so long now.
2312 * Search the available kegs for slabs. Be careful to hold the
2313 * correct lock while calling into the keg layer.
2315 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2316 keg = klink->kl_keg;
2317 keg_relock(keg, zone);
2318 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2319 slab = keg_fetch_slab(keg, zone, flags);
2323 if (keg->uk_flags & UMA_ZFLAG_FULL)
2327 zone_relock(zone, keg);
2329 if (rflags & (M_NOWAIT | M_NOVM))
2333 * All kegs are full. XXX We can't atomically check all kegs
2334 * and sleep so just sleep for a short period and retry.
2336 if (full && !empty) {
2337 zone->uz_flags |= UMA_ZFLAG_FULL;
2339 msleep(zone, zone->uz_lock, PVM, "zonelimit", hz/100);
2340 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2348 slab_alloc_item(uma_zone_t zone, uma_slab_t slab)
2351 uma_slabrefcnt_t slabref;
2356 mtx_assert(&keg->uk_lock, MA_OWNED);
2358 freei = slab->us_firstfree;
2359 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2360 slabref = (uma_slabrefcnt_t)slab;
2361 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2363 slab->us_firstfree = slab->us_freelist[freei].us_item;
2365 item = slab->us_data + (keg->uk_rsize * freei);
2367 slab->us_freecount--;
2370 uma_dbg_alloc(zone, slab, item);
2372 /* Move this slab to the full list */
2373 if (slab->us_freecount == 0) {
2374 LIST_REMOVE(slab, us_link);
2375 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2382 zone_alloc_bucket(uma_zone_t zone, int flags)
2384 uma_bucket_t bucket;
2388 int max, origflags = flags;
2391 * Try this zone's free list first so we don't allocate extra buckets.
2393 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2394 KASSERT(bucket->ub_cnt == 0,
2395 ("zone_alloc_bucket: Bucket on free list is not empty."));
2396 LIST_REMOVE(bucket, ub_link);
2400 bflags = (flags & ~M_ZERO);
2401 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2405 bucket = bucket_alloc(zone->uz_count, bflags);
2409 if (bucket == NULL) {
2415 * This code is here to limit the number of simultaneous bucket fills
2416 * for any given zone to the number of per cpu caches in this zone. This
2417 * is done so that we don't allocate more memory than we really need.
2419 if (zone->uz_fills >= mp_ncpus)
2425 max = MIN(bucket->ub_entries, zone->uz_count);
2426 /* Try to keep the buckets totally full */
2427 saved = bucket->ub_cnt;
2430 while (bucket->ub_cnt < max &&
2431 (slab = zone->uz_slab(zone, keg, flags)) != NULL) {
2433 while (slab->us_freecount && bucket->ub_cnt < max) {
2434 bucket->ub_bucket[bucket->ub_cnt++] =
2435 slab_alloc_item(zone, slab);
2438 /* Don't block on the next fill */
2442 zone_relock(zone, keg);
2445 * We unlock here because we need to call the zone's init.
2446 * It should be safe to unlock because the slab dealt with
2447 * above is already on the appropriate list within the keg
2448 * and the bucket we filled is not yet on any list, so we
2451 if (zone->uz_init != NULL) {
2455 for (i = saved; i < bucket->ub_cnt; i++)
2456 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2460 * If we couldn't initialize the whole bucket, put the
2461 * rest back onto the freelist.
2463 if (i != bucket->ub_cnt) {
2466 for (j = i; j < bucket->ub_cnt; j++) {
2467 zone_free_item(zone, bucket->ub_bucket[j],
2468 NULL, SKIP_FINI, 0);
2470 bucket->ub_bucket[j] = NULL;
2479 if (bucket->ub_cnt != 0) {
2480 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2487 bucket_free(bucket);
2492 * Allocates an item for an internal zone
2495 * zone The zone to alloc for.
2496 * udata The data to be passed to the constructor.
2497 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2500 * NULL if there is no memory and M_NOWAIT is set
2501 * An item if successful
2505 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2512 #ifdef UMA_DEBUG_ALLOC
2513 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2517 slab = zone->uz_slab(zone, NULL, flags);
2524 item = slab_alloc_item(zone, slab);
2526 zone_relock(zone, slab->us_keg);
2531 * We have to call both the zone's init (not the keg's init)
2532 * and the zone's ctor. This is because the item is going from
2533 * a keg slab directly to the user, and the user is expecting it
2534 * to be both zone-init'd as well as zone-ctor'd.
2536 if (zone->uz_init != NULL) {
2537 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2538 zone_free_item(zone, item, udata, SKIP_FINI,
2539 ZFREE_STATFAIL | ZFREE_STATFREE);
2543 if (zone->uz_ctor != NULL) {
2544 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2545 zone_free_item(zone, item, udata, SKIP_DTOR,
2546 ZFREE_STATFAIL | ZFREE_STATFREE);
2551 bzero(item, zone->uz_size);
2558 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2561 uma_bucket_t bucket;
2565 #ifdef UMA_DEBUG_ALLOC_1
2566 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2568 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2571 /* uma_zfree(..., NULL) does nothing, to match free(9). */
2574 #ifdef DEBUG_MEMGUARD
2575 if (is_memguard_addr(item)) {
2576 if (zone->uz_dtor != NULL && zone->uz_dtor != mtrash_dtor)
2577 zone->uz_dtor(item, zone->uz_size, udata);
2578 if (zone->uz_fini != NULL && zone->uz_fini != mtrash_fini)
2579 zone->uz_fini(item, zone->uz_size);
2580 memguard_free(item);
2585 zone->uz_dtor(item, zone->uz_size, udata);
2589 if (zone->uz_flags & UMA_ZONE_MALLOC)
2590 uma_dbg_free(zone, udata, item);
2592 uma_dbg_free(zone, NULL, item);
2596 * The race here is acceptable. If we miss it we'll just have to wait
2597 * a little longer for the limits to be reset.
2599 if (zone->uz_flags & UMA_ZFLAG_FULL)
2600 goto zfree_internal;
2603 * If possible, free to the per-CPU cache. There are two
2604 * requirements for safe access to the per-CPU cache: (1) the thread
2605 * accessing the cache must not be preempted or yield during access,
2606 * and (2) the thread must not migrate CPUs without switching which
2607 * cache it accesses. We rely on a critical section to prevent
2608 * preemption and migration. We release the critical section in
2609 * order to acquire the zone mutex if we are unable to free to the
2610 * current cache; when we re-acquire the critical section, we must
2611 * detect and handle migration if it has occurred.
2616 cache = &zone->uz_cpu[cpu];
2619 bucket = cache->uc_freebucket;
2623 * Do we have room in our bucket? It is OK for this uz count
2624 * check to be slightly out of sync.
2627 if (bucket->ub_cnt < bucket->ub_entries) {
2628 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2629 ("uma_zfree: Freeing to non free bucket index."));
2630 bucket->ub_bucket[bucket->ub_cnt] = item;
2635 } else if (cache->uc_allocbucket) {
2636 #ifdef UMA_DEBUG_ALLOC
2637 printf("uma_zfree: Swapping buckets.\n");
2640 * We have run out of space in our freebucket.
2641 * See if we can switch with our alloc bucket.
2643 if (cache->uc_allocbucket->ub_cnt <
2644 cache->uc_freebucket->ub_cnt) {
2645 bucket = cache->uc_freebucket;
2646 cache->uc_freebucket = cache->uc_allocbucket;
2647 cache->uc_allocbucket = bucket;
2653 * We can get here for two reasons:
2655 * 1) The buckets are NULL
2656 * 2) The alloc and free buckets are both somewhat full.
2658 * We must go back the zone, which requires acquiring the zone lock,
2659 * which in turn means we must release and re-acquire the critical
2660 * section. Since the critical section is released, we may be
2661 * preempted or migrate. As such, make sure not to maintain any
2662 * thread-local state specific to the cache from prior to releasing
2663 * the critical section.
2669 cache = &zone->uz_cpu[cpu];
2670 if (cache->uc_freebucket != NULL) {
2671 if (cache->uc_freebucket->ub_cnt <
2672 cache->uc_freebucket->ub_entries) {
2676 if (cache->uc_allocbucket != NULL &&
2677 (cache->uc_allocbucket->ub_cnt <
2678 cache->uc_freebucket->ub_cnt)) {
2684 /* Since we have locked the zone we may as well send back our stats */
2685 zone->uz_allocs += cache->uc_allocs;
2686 cache->uc_allocs = 0;
2687 zone->uz_frees += cache->uc_frees;
2688 cache->uc_frees = 0;
2690 bucket = cache->uc_freebucket;
2691 cache->uc_freebucket = NULL;
2693 /* Can we throw this on the zone full list? */
2694 if (bucket != NULL) {
2695 #ifdef UMA_DEBUG_ALLOC
2696 printf("uma_zfree: Putting old bucket on the free list.\n");
2698 /* ub_cnt is pointing to the last free item */
2699 KASSERT(bucket->ub_cnt != 0,
2700 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2701 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2704 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2705 LIST_REMOVE(bucket, ub_link);
2707 cache->uc_freebucket = bucket;
2710 /* We are no longer associated with this CPU. */
2713 /* And the zone.. */
2716 #ifdef UMA_DEBUG_ALLOC
2717 printf("uma_zfree: Allocating new free bucket.\n");
2721 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2723 bucket = bucket_alloc(zone->uz_count, bflags);
2726 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2733 * If nothing else caught this, we'll just do an internal free.
2736 zone_free_item(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2742 * Frees an item to an INTERNAL zone or allocates a free bucket
2745 * zone The zone to free to
2746 * item The item we're freeing
2747 * udata User supplied data for the dtor
2748 * skip Skip dtors and finis
2751 zone_free_item(uma_zone_t zone, void *item, void *udata,
2752 enum zfreeskip skip, int flags)
2755 uma_slabrefcnt_t slabref;
2761 if (skip < SKIP_DTOR && zone->uz_dtor)
2762 zone->uz_dtor(item, zone->uz_size, udata);
2764 if (skip < SKIP_FINI && zone->uz_fini)
2765 zone->uz_fini(item, zone->uz_size);
2769 if (flags & ZFREE_STATFAIL)
2771 if (flags & ZFREE_STATFREE)
2774 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2775 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2776 keg = zone_first_keg(zone); /* Must only be one. */
2777 if (zone->uz_flags & UMA_ZONE_HASH) {
2778 slab = hash_sfind(&keg->uk_hash, mem);
2780 mem += keg->uk_pgoff;
2781 slab = (uma_slab_t)mem;
2784 /* This prevents redundant lookups via free(). */
2785 if ((zone->uz_flags & UMA_ZONE_MALLOC) && udata != NULL)
2786 slab = (uma_slab_t)udata;
2788 slab = vtoslab((vm_offset_t)item);
2790 keg_relock(keg, zone);
2792 MPASS(keg == slab->us_keg);
2794 /* Do we need to remove from any lists? */
2795 if (slab->us_freecount+1 == keg->uk_ipers) {
2796 LIST_REMOVE(slab, us_link);
2797 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2798 } else if (slab->us_freecount == 0) {
2799 LIST_REMOVE(slab, us_link);
2800 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2803 /* Slab management stuff */
2804 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2809 uma_dbg_free(zone, slab, item);
2812 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2813 slabref = (uma_slabrefcnt_t)slab;
2814 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2816 slab->us_freelist[freei].us_item = slab->us_firstfree;
2818 slab->us_firstfree = freei;
2819 slab->us_freecount++;
2821 /* Zone statistics */
2825 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2826 if (keg->uk_pages < keg->uk_maxpages) {
2827 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2832 * We can handle one more allocation. Since we're clearing ZFLAG_FULL,
2833 * wake up all procs blocked on pages. This should be uncommon, so
2834 * keeping this simple for now (rather than adding count of blocked
2840 zone_relock(zone, keg);
2841 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2850 uma_zone_set_max(uma_zone_t zone, int nitems)
2855 keg = zone_first_keg(zone);
2856 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2857 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2858 keg->uk_maxpages += keg->uk_ppera;
2859 nitems = keg->uk_maxpages * keg->uk_ipers;
2867 uma_zone_get_max(uma_zone_t zone)
2873 keg = zone_first_keg(zone);
2874 nitems = keg->uk_maxpages * keg->uk_ipers;
2882 uma_zone_get_cur(uma_zone_t zone)
2888 nitems = zone->uz_allocs - zone->uz_frees;
2891 * See the comment in sysctl_vm_zone_stats() regarding the
2892 * safety of accessing the per-cpu caches. With the zone lock
2893 * held, it is safe, but can potentially result in stale data.
2895 nitems += zone->uz_cpu[i].uc_allocs -
2896 zone->uz_cpu[i].uc_frees;
2900 return (nitems < 0 ? 0 : nitems);
2905 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2910 keg = zone_first_keg(zone);
2911 KASSERT(keg->uk_pages == 0,
2912 ("uma_zone_set_init on non-empty keg"));
2913 keg->uk_init = uminit;
2919 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2924 keg = zone_first_keg(zone);
2925 KASSERT(keg->uk_pages == 0,
2926 ("uma_zone_set_fini on non-empty keg"));
2927 keg->uk_fini = fini;
2933 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2936 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2937 ("uma_zone_set_zinit on non-empty keg"));
2938 zone->uz_init = zinit;
2944 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2947 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2948 ("uma_zone_set_zfini on non-empty keg"));
2949 zone->uz_fini = zfini;
2954 /* XXX uk_freef is not actually used with the zone locked */
2956 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2960 zone_first_keg(zone)->uk_freef = freef;
2965 /* XXX uk_allocf is not actually used with the zone locked */
2967 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2972 keg = zone_first_keg(zone);
2973 keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2974 keg->uk_allocf = allocf;
2980 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2986 keg = zone_first_keg(zone);
2987 pages = count / keg->uk_ipers;
2989 if (pages * keg->uk_ipers < count)
2992 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2997 obj = vm_object_allocate(OBJT_PHYS, pages);
2999 VM_OBJECT_LOCK_INIT(obj, "uma object");
3000 _vm_object_allocate(OBJT_PHYS, pages, obj);
3005 keg->uk_maxpages = pages;
3006 keg->uk_allocf = obj_alloc;
3007 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
3014 uma_prealloc(uma_zone_t zone, int items)
3020 keg = zone_first_keg(zone);
3022 slabs = items / keg->uk_ipers;
3023 if (slabs * keg->uk_ipers < items)
3026 slab = keg_alloc_slab(keg, zone, M_WAITOK);
3029 MPASS(slab->us_keg == keg);
3030 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
3038 uma_find_refcnt(uma_zone_t zone, void *item)
3040 uma_slabrefcnt_t slabref;
3045 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
3047 keg = slabref->us_keg;
3048 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
3049 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3050 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
3052 refcnt = &slabref->us_freelist[idx].us_refcnt;
3061 printf("UMA: vm asked us to release pages!\n");
3064 zone_foreach(zone_drain);
3066 * Some slabs may have been freed but this zone will be visited early
3067 * we visit again so that we can free pages that are empty once other
3068 * zones are drained. We have to do the same for buckets.
3070 zone_drain(slabzone);
3071 zone_drain(slabrefzone);
3072 bucket_zone_drain();
3077 uma_zone_exhausted(uma_zone_t zone)
3082 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3088 uma_zone_exhausted_nolock(uma_zone_t zone)
3090 return (zone->uz_flags & UMA_ZFLAG_FULL);
3094 uma_large_malloc(int size, int wait)
3100 slab = zone_alloc_item(slabzone, NULL, wait);
3103 mem = page_alloc(NULL, size, &flags, wait);
3105 vsetslab((vm_offset_t)mem, slab);
3106 slab->us_data = mem;
3107 slab->us_flags = flags | UMA_SLAB_MALLOC;
3108 slab->us_size = size;
3110 zone_free_item(slabzone, slab, NULL, SKIP_NONE,
3111 ZFREE_STATFAIL | ZFREE_STATFREE);
3118 uma_large_free(uma_slab_t slab)
3120 vsetobj((vm_offset_t)slab->us_data, kmem_object);
3121 page_free(slab->us_data, slab->us_size, slab->us_flags);
3122 zone_free_item(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
3126 uma_print_stats(void)
3128 zone_foreach(uma_print_zone);
3132 slab_print(uma_slab_t slab)
3134 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
3135 slab->us_keg, slab->us_data, slab->us_freecount,
3136 slab->us_firstfree);
3140 cache_print(uma_cache_t cache)
3142 printf("alloc: %p(%d), free: %p(%d)\n",
3143 cache->uc_allocbucket,
3144 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3145 cache->uc_freebucket,
3146 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3150 uma_print_keg(uma_keg_t keg)
3154 printf("keg: %s(%p) size %d(%d) flags %d ipers %d ppera %d "
3155 "out %d free %d limit %d\n",
3156 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3157 keg->uk_ipers, keg->uk_ppera,
3158 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3159 (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3160 printf("Part slabs:\n");
3161 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3163 printf("Free slabs:\n");
3164 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3166 printf("Full slabs:\n");
3167 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3172 uma_print_zone(uma_zone_t zone)
3178 printf("zone: %s(%p) size %d flags %d\n",
3179 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3180 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3181 uma_print_keg(kl->kl_keg);
3183 cache = &zone->uz_cpu[i];
3184 printf("CPU %d Cache:\n", i);
3191 * Generate statistics across both the zone and its per-cpu cache's. Return
3192 * desired statistics if the pointer is non-NULL for that statistic.
3194 * Note: does not update the zone statistics, as it can't safely clear the
3195 * per-CPU cache statistic.
3197 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3198 * safe from off-CPU; we should modify the caches to track this information
3199 * directly so that we don't have to.
3202 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
3203 u_int64_t *freesp, u_int64_t *sleepsp)
3206 u_int64_t allocs, frees, sleeps;
3209 allocs = frees = sleeps = 0;
3212 cache = &z->uz_cpu[cpu];
3213 if (cache->uc_allocbucket != NULL)
3214 cachefree += cache->uc_allocbucket->ub_cnt;
3215 if (cache->uc_freebucket != NULL)
3216 cachefree += cache->uc_freebucket->ub_cnt;
3217 allocs += cache->uc_allocs;
3218 frees += cache->uc_frees;
3220 allocs += z->uz_allocs;
3221 frees += z->uz_frees;
3222 sleeps += z->uz_sleeps;
3223 if (cachefreep != NULL)
3224 *cachefreep = cachefree;
3225 if (allocsp != NULL)
3229 if (sleepsp != NULL)
3235 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3243 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3244 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3247 mtx_unlock(&uma_mtx);
3248 return (sysctl_handle_int(oidp, &count, 0, req));
3252 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3254 struct uma_stream_header ush;
3255 struct uma_type_header uth;
3256 struct uma_percpu_stat ups;
3257 uma_bucket_t bucket;
3264 int count, error, i;
3266 error = sysctl_wire_old_buffer(req, 0);
3269 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3273 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3274 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3279 * Insert stream header.
3281 bzero(&ush, sizeof(ush));
3282 ush.ush_version = UMA_STREAM_VERSION;
3283 ush.ush_maxcpus = (mp_maxid + 1);
3284 ush.ush_count = count;
3285 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3287 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3288 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3289 bzero(&uth, sizeof(uth));
3291 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3292 uth.uth_align = kz->uk_align;
3293 uth.uth_size = kz->uk_size;
3294 uth.uth_rsize = kz->uk_rsize;
3295 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3297 uth.uth_maxpages += k->uk_maxpages;
3298 uth.uth_pages += k->uk_pages;
3299 uth.uth_keg_free += k->uk_free;
3300 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3305 * A zone is secondary is it is not the first entry
3306 * on the keg's zone list.
3308 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3309 (LIST_FIRST(&kz->uk_zones) != z))
3310 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3312 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3313 uth.uth_zone_free += bucket->ub_cnt;
3314 uth.uth_allocs = z->uz_allocs;
3315 uth.uth_frees = z->uz_frees;
3316 uth.uth_fails = z->uz_fails;
3317 uth.uth_sleeps = z->uz_sleeps;
3318 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
3320 * While it is not normally safe to access the cache
3321 * bucket pointers while not on the CPU that owns the
3322 * cache, we only allow the pointers to be exchanged
3323 * without the zone lock held, not invalidated, so
3324 * accept the possible race associated with bucket
3325 * exchange during monitoring.
3327 for (i = 0; i < (mp_maxid + 1); i++) {
3328 bzero(&ups, sizeof(ups));
3329 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3333 cache = &z->uz_cpu[i];
3334 if (cache->uc_allocbucket != NULL)
3335 ups.ups_cache_free +=
3336 cache->uc_allocbucket->ub_cnt;
3337 if (cache->uc_freebucket != NULL)
3338 ups.ups_cache_free +=
3339 cache->uc_freebucket->ub_cnt;
3340 ups.ups_allocs = cache->uc_allocs;
3341 ups.ups_frees = cache->uc_frees;
3343 (void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
3348 mtx_unlock(&uma_mtx);
3349 error = sbuf_finish(&sbuf);
3355 DB_SHOW_COMMAND(uma, db_show_uma)
3357 u_int64_t allocs, frees, sleeps;
3358 uma_bucket_t bucket;
3363 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3364 "Requests", "Sleeps");
3365 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3366 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3367 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3368 allocs = z->uz_allocs;
3369 frees = z->uz_frees;
3370 sleeps = z->uz_sleeps;
3373 uma_zone_sumstat(z, &cachefree, &allocs,
3375 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3376 (LIST_FIRST(&kz->uk_zones) != z)))
3377 cachefree += kz->uk_free;
3378 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3379 cachefree += bucket->ub_cnt;
3380 db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name,
3381 (uintmax_t)kz->uk_size,
3382 (intmax_t)(allocs - frees), cachefree,
3383 (uintmax_t)allocs, sleeps);