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"
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
72 #include <sys/mutex.h>
76 #include <sys/vmmeter.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_extern.h>
86 #include <vm/uma_int.h>
87 #include <vm/uma_dbg.h>
89 #include <machine/vmparam.h>
94 * This is the zone and keg from which all zones are spawned. The idea is that
95 * even the zone & keg heads are allocated from the allocator, so we use the
96 * bss section to bootstrap us.
98 static struct uma_keg masterkeg;
99 static struct uma_zone masterzone_k;
100 static struct uma_zone masterzone_z;
101 static uma_zone_t kegs = &masterzone_k;
102 static uma_zone_t zones = &masterzone_z;
104 /* This is the zone from which all of uma_slab_t's are allocated. */
105 static uma_zone_t slabzone;
106 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
109 * The initial hash tables come out of this zone so they can be allocated
110 * prior to malloc coming up.
112 static uma_zone_t hashzone;
114 /* The boot-time adjusted value for cache line alignment. */
115 static int uma_align_cache = 64 - 1;
117 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
120 * Are we allowed to allocate buckets?
122 static int bucketdisable = 1;
124 /* Linked list of all kegs in the system */
125 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(&uma_kegs);
127 /* This mutex protects the keg list */
128 static struct mtx uma_mtx;
130 /* Linked list of boot time pages */
131 static LIST_HEAD(,uma_slab) uma_boot_pages =
132 LIST_HEAD_INITIALIZER(&uma_boot_pages);
134 /* This mutex protects the boot time pages list */
135 static struct mtx uma_boot_pages_mtx;
137 /* Is the VM done starting up? */
138 static int booted = 0;
140 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
141 static u_int uma_max_ipers;
142 static u_int uma_max_ipers_ref;
145 * This is the handle used to schedule events that need to happen
146 * outside of the allocation fast path.
148 static struct callout uma_callout;
149 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
152 * This structure is passed as the zone ctor arg so that I don't have to create
153 * a special allocation function just for zones.
155 struct uma_zctor_args {
167 struct uma_kctor_args {
176 struct uma_bucket_zone {
182 #define BUCKET_MAX 128
184 struct uma_bucket_zone bucket_zones[] = {
185 { NULL, "16 Bucket", 16 },
186 { NULL, "32 Bucket", 32 },
187 { NULL, "64 Bucket", 64 },
188 { NULL, "128 Bucket", 128 },
192 #define BUCKET_SHIFT 4
193 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
196 * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
197 * of approximately the right size.
199 static uint8_t bucket_size[BUCKET_ZONES];
202 * Flags and enumerations to be passed to internal functions.
204 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
206 #define ZFREE_STATFAIL 0x00000001 /* Update zone failure statistic. */
207 #define ZFREE_STATFREE 0x00000002 /* Update zone free statistic. */
211 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
212 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
213 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
214 static void page_free(void *, int, u_int8_t);
215 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
216 static void cache_drain(uma_zone_t);
217 static void bucket_drain(uma_zone_t, uma_bucket_t);
218 static void bucket_cache_drain(uma_zone_t zone);
219 static int keg_ctor(void *, int, void *, int);
220 static void keg_dtor(void *, int, void *);
221 static int zone_ctor(void *, int, void *, int);
222 static void zone_dtor(void *, int, void *);
223 static int zero_init(void *, int, int);
224 static void keg_small_init(uma_keg_t keg);
225 static void keg_large_init(uma_keg_t keg);
226 static void zone_foreach(void (*zfunc)(uma_zone_t));
227 static void zone_timeout(uma_zone_t zone);
228 static int hash_alloc(struct uma_hash *);
229 static int hash_expand(struct uma_hash *, struct uma_hash *);
230 static void hash_free(struct uma_hash *hash);
231 static void uma_timeout(void *);
232 static void uma_startup3(void);
233 static void *zone_alloc_item(uma_zone_t, void *, int);
234 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip,
236 static void bucket_enable(void);
237 static void bucket_init(void);
238 static uma_bucket_t bucket_alloc(int, int);
239 static void bucket_free(uma_bucket_t);
240 static void bucket_zone_drain(void);
241 static int zone_alloc_bucket(uma_zone_t zone, int flags);
242 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
243 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
244 static void *slab_alloc_item(uma_zone_t zone, uma_slab_t slab);
245 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
246 uma_fini fini, int align, u_int32_t flags);
247 static inline void zone_relock(uma_zone_t zone, uma_keg_t keg);
248 static inline void keg_relock(uma_keg_t keg, uma_zone_t zone);
250 void uma_print_zone(uma_zone_t);
251 void uma_print_stats(void);
252 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
253 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
255 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
257 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
258 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
260 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
261 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
264 * This routine checks to see whether or not it's safe to enable buckets.
270 if (cnt.v_free_count < cnt.v_free_min)
277 * Initialize bucket_zones, the array of zones of buckets of various sizes.
279 * For each zone, calculate the memory required for each bucket, consisting
280 * of the header and an array of pointers. Initialize bucket_size[] to point
281 * the range of appropriate bucket sizes at the zone.
286 struct uma_bucket_zone *ubz;
290 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
293 ubz = &bucket_zones[j];
294 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
295 size += sizeof(void *) * ubz->ubz_entries;
296 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
297 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
298 UMA_ZFLAG_INTERNAL | UMA_ZFLAG_BUCKET);
299 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
300 bucket_size[i >> BUCKET_SHIFT] = j;
305 * Given a desired number of entries for a bucket, return the zone from which
306 * to allocate the bucket.
308 static struct uma_bucket_zone *
309 bucket_zone_lookup(int entries)
313 idx = howmany(entries, 1 << BUCKET_SHIFT);
314 return (&bucket_zones[bucket_size[idx]]);
318 bucket_alloc(int entries, int bflags)
320 struct uma_bucket_zone *ubz;
324 * This is to stop us from allocating per cpu buckets while we're
325 * running out of vm.boot_pages. Otherwise, we would exhaust the
326 * boot pages. This also prevents us from allocating buckets in
327 * low memory situations.
332 ubz = bucket_zone_lookup(entries);
333 bucket = zone_alloc_item(ubz->ubz_zone, NULL, bflags);
336 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
339 bucket->ub_entries = ubz->ubz_entries;
346 bucket_free(uma_bucket_t bucket)
348 struct uma_bucket_zone *ubz;
350 ubz = bucket_zone_lookup(bucket->ub_entries);
351 zone_free_item(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
356 bucket_zone_drain(void)
358 struct uma_bucket_zone *ubz;
360 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
361 zone_drain(ubz->ubz_zone);
364 static inline uma_keg_t
365 zone_first_keg(uma_zone_t zone)
368 return (LIST_FIRST(&zone->uz_kegs)->kl_keg);
372 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
376 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
377 kegfn(klink->kl_keg);
381 * Routine called by timeout which is used to fire off some time interval
382 * based calculations. (stats, hash size, etc.)
391 uma_timeout(void *unused)
394 zone_foreach(zone_timeout);
396 /* Reschedule this event */
397 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
401 * Routine to perform timeout driven calculations. This expands the
402 * hashes and does per cpu statistics aggregation.
407 keg_timeout(uma_keg_t keg)
412 * Expand the keg hash table.
414 * This is done if the number of slabs is larger than the hash size.
415 * What I'm trying to do here is completely reduce collisions. This
416 * may be a little aggressive. Should I allow for two collisions max?
418 if (keg->uk_flags & UMA_ZONE_HASH &&
419 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
420 struct uma_hash newhash;
421 struct uma_hash oldhash;
425 * This is so involved because allocating and freeing
426 * while the keg lock is held will lead to deadlock.
427 * I have to do everything in stages and check for
430 newhash = keg->uk_hash;
432 ret = hash_alloc(&newhash);
435 if (hash_expand(&keg->uk_hash, &newhash)) {
436 oldhash = keg->uk_hash;
437 keg->uk_hash = newhash;
450 zone_timeout(uma_zone_t zone)
453 zone_foreach_keg(zone, &keg_timeout);
457 * Allocate and zero fill the next sized hash table from the appropriate
461 * hash A new hash structure with the old hash size in uh_hashsize
464 * 1 on sucess and 0 on failure.
467 hash_alloc(struct uma_hash *hash)
472 oldsize = hash->uh_hashsize;
474 /* We're just going to go to a power of two greater */
476 hash->uh_hashsize = oldsize * 2;
477 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
478 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
479 M_UMAHASH, M_NOWAIT);
481 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
482 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
484 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
486 if (hash->uh_slab_hash) {
487 bzero(hash->uh_slab_hash, alloc);
488 hash->uh_hashmask = hash->uh_hashsize - 1;
496 * Expands the hash table for HASH zones. This is done from zone_timeout
497 * to reduce collisions. This must not be done in the regular allocation
498 * path, otherwise, we can recurse on the vm while allocating pages.
501 * oldhash The hash you want to expand
502 * newhash The hash structure for the new table
510 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
516 if (!newhash->uh_slab_hash)
519 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
523 * I need to investigate hash algorithms for resizing without a
527 for (i = 0; i < oldhash->uh_hashsize; i++)
528 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
529 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
530 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
531 hval = UMA_HASH(newhash, slab->us_data);
532 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
540 * Free the hash bucket to the appropriate backing store.
543 * slab_hash The hash bucket we're freeing
544 * hashsize The number of entries in that hash bucket
550 hash_free(struct uma_hash *hash)
552 if (hash->uh_slab_hash == NULL)
554 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
555 zone_free_item(hashzone,
556 hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
558 free(hash->uh_slab_hash, M_UMAHASH);
562 * Frees all outstanding items in a bucket
565 * zone The zone to free to, must be unlocked.
566 * bucket The free/alloc bucket with items, cpu queue must be locked.
573 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
580 while (bucket->ub_cnt > 0) {
582 item = bucket->ub_bucket[bucket->ub_cnt];
584 bucket->ub_bucket[bucket->ub_cnt] = NULL;
585 KASSERT(item != NULL,
586 ("bucket_drain: botched ptr, item is NULL"));
588 zone_free_item(zone, item, NULL, SKIP_DTOR, 0);
593 * Drains the per cpu caches for a zone.
595 * NOTE: This may only be called while the zone is being turn down, and not
596 * during normal operation. This is necessary in order that we do not have
597 * to migrate CPUs to drain the per-CPU caches.
600 * zone The zone to drain, must be unlocked.
606 cache_drain(uma_zone_t zone)
612 * XXX: It is safe to not lock the per-CPU caches, because we're
613 * tearing down the zone anyway. I.e., there will be no further use
614 * of the caches at this point.
616 * XXX: It would good to be able to assert that the zone is being
617 * torn down to prevent improper use of cache_drain().
619 * XXX: We lock the zone before passing into bucket_cache_drain() as
620 * it is used elsewhere. Should the tear-down path be made special
621 * there in some form?
623 for (cpu = 0; cpu <= mp_maxid; cpu++) {
626 cache = &zone->uz_cpu[cpu];
627 bucket_drain(zone, cache->uc_allocbucket);
628 bucket_drain(zone, cache->uc_freebucket);
629 if (cache->uc_allocbucket != NULL)
630 bucket_free(cache->uc_allocbucket);
631 if (cache->uc_freebucket != NULL)
632 bucket_free(cache->uc_freebucket);
633 cache->uc_allocbucket = cache->uc_freebucket = NULL;
636 bucket_cache_drain(zone);
641 * Drain the cached buckets from a zone. Expects a locked zone on entry.
644 bucket_cache_drain(uma_zone_t zone)
649 * Drain the bucket queues and free the buckets, we just keep two per
652 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
653 LIST_REMOVE(bucket, ub_link);
655 bucket_drain(zone, bucket);
660 /* Now we do the free queue.. */
661 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
662 LIST_REMOVE(bucket, ub_link);
668 * Frees pages from a keg back to the system. This is done on demand from
669 * the pageout daemon.
674 keg_drain(uma_keg_t keg)
676 struct slabhead freeslabs = { 0 };
684 * We don't want to take pages from statically allocated kegs at this
687 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
691 printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
694 if (keg->uk_free == 0)
697 slab = LIST_FIRST(&keg->uk_free_slab);
699 n = LIST_NEXT(slab, us_link);
701 /* We have no where to free these to */
702 if (slab->us_flags & UMA_SLAB_BOOT) {
707 LIST_REMOVE(slab, us_link);
708 keg->uk_pages -= keg->uk_ppera;
709 keg->uk_free -= keg->uk_ipers;
711 if (keg->uk_flags & UMA_ZONE_HASH)
712 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
714 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
721 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
722 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
724 for (i = 0; i < keg->uk_ipers; i++)
726 slab->us_data + (keg->uk_rsize * i),
728 flags = slab->us_flags;
731 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
734 if (flags & UMA_SLAB_KMEM)
736 else if (flags & UMA_SLAB_KERNEL)
740 for (i = 0; i < keg->uk_ppera; i++)
741 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
744 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
745 zone_free_item(keg->uk_slabzone, slab, NULL,
746 SKIP_NONE, ZFREE_STATFREE);
748 printf("%s: Returning %d bytes.\n",
749 keg->uk_name, UMA_SLAB_SIZE * keg->uk_ppera);
751 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
756 zone_drain_wait(uma_zone_t zone, int waitok)
760 * Set draining to interlock with zone_dtor() so we can release our
761 * locks as we go. Only dtor() should do a WAITOK call since it
762 * is the only call that knows the structure will still be available
766 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
767 if (waitok == M_NOWAIT)
769 mtx_unlock(&uma_mtx);
770 msleep(zone, zone->uz_lock, PVM, "zonedrain", 1);
773 zone->uz_flags |= UMA_ZFLAG_DRAINING;
774 bucket_cache_drain(zone);
777 * The DRAINING flag protects us from being freed while
778 * we're running. Normally the uma_mtx would protect us but we
779 * must be able to release and acquire the right lock for each keg.
781 zone_foreach_keg(zone, &keg_drain);
783 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
790 zone_drain(uma_zone_t zone)
793 zone_drain_wait(zone, M_NOWAIT);
797 * Allocate a new slab for a keg. This does not insert the slab onto a list.
800 * wait Shall we wait?
803 * The slab that was allocated or NULL if there is no memory and the
804 * caller specified M_NOWAIT.
807 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
809 uma_slabrefcnt_t slabref;
816 mtx_assert(&keg->uk_lock, MA_OWNED);
820 printf("slab_zalloc: Allocating a new slab for %s\n", keg->uk_name);
822 allocf = keg->uk_allocf;
825 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
826 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
834 * This reproduces the old vm_zone behavior of zero filling pages the
835 * first time they are added to a zone.
837 * Malloced items are zeroed in uma_zalloc.
840 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
845 /* zone is passed for legacy reasons. */
846 mem = allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE, &flags, wait);
848 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
849 zone_free_item(keg->uk_slabzone, slab, NULL,
850 SKIP_NONE, ZFREE_STATFREE);
855 /* Point the slab into the allocated memory */
856 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
857 slab = (uma_slab_t )(mem + keg->uk_pgoff);
859 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
860 for (i = 0; i < keg->uk_ppera; i++)
861 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
865 slab->us_freecount = keg->uk_ipers;
866 slab->us_firstfree = 0;
867 slab->us_flags = flags;
869 if (keg->uk_flags & UMA_ZONE_REFCNT) {
870 slabref = (uma_slabrefcnt_t)slab;
871 for (i = 0; i < keg->uk_ipers; i++) {
872 slabref->us_freelist[i].us_refcnt = 0;
873 slabref->us_freelist[i].us_item = i+1;
876 for (i = 0; i < keg->uk_ipers; i++)
877 slab->us_freelist[i].us_item = i+1;
880 if (keg->uk_init != NULL) {
881 for (i = 0; i < keg->uk_ipers; i++)
882 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
883 keg->uk_size, wait) != 0)
885 if (i != keg->uk_ipers) {
886 if (keg->uk_fini != NULL) {
887 for (i--; i > -1; i--)
888 keg->uk_fini(slab->us_data +
892 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
895 if (flags & UMA_SLAB_KMEM)
897 else if (flags & UMA_SLAB_KERNEL)
901 for (i = 0; i < keg->uk_ppera; i++)
902 vsetobj((vm_offset_t)mem +
903 (i * PAGE_SIZE), obj);
905 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
906 zone_free_item(keg->uk_slabzone, slab,
907 NULL, SKIP_NONE, ZFREE_STATFREE);
908 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
916 if (keg->uk_flags & UMA_ZONE_HASH)
917 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
919 keg->uk_pages += keg->uk_ppera;
920 keg->uk_free += keg->uk_ipers;
926 * This function is intended to be used early on in place of page_alloc() so
927 * that we may use the boot time page cache to satisfy allocations before
931 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
936 keg = zone_first_keg(zone);
939 * Check our small startup cache to see if it has pages remaining.
941 mtx_lock(&uma_boot_pages_mtx);
942 if ((tmps = LIST_FIRST(&uma_boot_pages)) != NULL) {
943 LIST_REMOVE(tmps, us_link);
944 mtx_unlock(&uma_boot_pages_mtx);
945 *pflag = tmps->us_flags;
946 return (tmps->us_data);
948 mtx_unlock(&uma_boot_pages_mtx);
950 panic("UMA: Increase vm.boot_pages");
952 * Now that we've booted reset these users to their real allocator.
954 #ifdef UMA_MD_SMALL_ALLOC
955 keg->uk_allocf = uma_small_alloc;
957 keg->uk_allocf = page_alloc;
959 return keg->uk_allocf(zone, bytes, pflag, wait);
963 * Allocates a number of pages from the system
966 * bytes The number of bytes requested
967 * wait Shall we wait?
970 * A pointer to the alloced memory or possibly
971 * NULL if M_NOWAIT is set.
974 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
976 void *p; /* Returned page */
978 *pflag = UMA_SLAB_KMEM;
979 p = (void *) kmem_malloc(kmem_map, bytes, wait);
985 * Allocates a number of pages from within an object
988 * bytes The number of bytes requested
989 * wait Shall we wait?
992 * A pointer to the alloced memory or possibly
993 * NULL if M_NOWAIT is set.
996 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
999 vm_offset_t retkva, zkva;
1001 int pages, startpages;
1004 keg = zone_first_keg(zone);
1005 object = keg->uk_obj;
1009 * This looks a little weird since we're getting one page at a time.
1011 VM_OBJECT_LOCK(object);
1012 p = TAILQ_LAST(&object->memq, pglist);
1013 pages = p != NULL ? p->pindex + 1 : 0;
1015 zkva = keg->uk_kva + pages * PAGE_SIZE;
1016 for (; bytes > 0; bytes -= PAGE_SIZE) {
1017 p = vm_page_alloc(object, pages,
1018 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
1020 if (pages != startpages)
1021 pmap_qremove(retkva, pages - startpages);
1022 while (pages != startpages) {
1024 p = TAILQ_LAST(&object->memq, pglist);
1025 vm_page_lock_queues();
1026 vm_page_unwire(p, 0);
1028 vm_page_unlock_queues();
1033 pmap_qenter(zkva, &p, 1);
1040 VM_OBJECT_UNLOCK(object);
1041 *flags = UMA_SLAB_PRIV;
1043 return ((void *)retkva);
1047 * Frees a number of pages to the system
1050 * mem A pointer to the memory to be freed
1051 * size The size of the memory being freed
1052 * flags The original p->us_flags field
1058 page_free(void *mem, int size, u_int8_t flags)
1062 if (flags & UMA_SLAB_KMEM)
1064 else if (flags & UMA_SLAB_KERNEL)
1067 panic("UMA: page_free used with invalid flags %d", flags);
1069 kmem_free(map, (vm_offset_t)mem, size);
1073 * Zero fill initializer
1075 * Arguments/Returns follow uma_init specifications
1078 zero_init(void *mem, int size, int flags)
1085 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1088 * keg The zone we should initialize
1094 keg_small_init(uma_keg_t keg)
1101 KASSERT(keg != NULL, ("Keg is null in keg_small_init"));
1102 rsize = keg->uk_size;
1104 if (rsize < UMA_SMALLEST_UNIT)
1105 rsize = UMA_SMALLEST_UNIT;
1106 if (rsize & keg->uk_align)
1107 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1109 keg->uk_rsize = rsize;
1112 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1113 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1114 shsize = sizeof(struct uma_slab_refcnt);
1116 rsize += UMA_FRITM_SZ; /* Account for linkage */
1117 shsize = sizeof(struct uma_slab);
1120 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1121 KASSERT(keg->uk_ipers != 0, ("keg_small_init: ipers is 0"));
1122 memused = keg->uk_ipers * rsize + shsize;
1123 wastedspace = UMA_SLAB_SIZE - memused;
1126 * We can't do OFFPAGE if we're internal or if we've been
1127 * asked to not go to the VM for buckets. If we do this we
1128 * may end up going to the VM (kmem_map) for slabs which we
1129 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1130 * result of UMA_ZONE_VM, which clearly forbids it.
1132 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1133 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1136 if ((wastedspace >= UMA_MAX_WASTE) &&
1137 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1138 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1139 KASSERT(keg->uk_ipers <= 255,
1140 ("keg_small_init: keg->uk_ipers too high!"));
1142 printf("UMA decided we need offpage slab headers for "
1143 "keg: %s, calculated wastedspace = %d, "
1144 "maximum wasted space allowed = %d, "
1145 "calculated ipers = %d, "
1146 "new wasted space = %d\n", keg->uk_name, wastedspace,
1147 UMA_MAX_WASTE, keg->uk_ipers,
1148 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1150 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1151 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1152 keg->uk_flags |= UMA_ZONE_HASH;
1157 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1158 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1162 * keg The keg we should initialize
1168 keg_large_init(uma_keg_t keg)
1172 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1173 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1174 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1176 pages = keg->uk_size / UMA_SLAB_SIZE;
1178 /* Account for remainder */
1179 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1182 keg->uk_ppera = pages;
1185 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1186 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1187 keg->uk_flags |= UMA_ZONE_HASH;
1189 keg->uk_rsize = keg->uk_size;
1193 keg_cachespread_init(uma_keg_t keg)
1200 alignsize = keg->uk_align + 1;
1201 rsize = keg->uk_size;
1203 * We want one item to start on every align boundary in a page. To
1204 * do this we will span pages. We will also extend the item by the
1205 * size of align if it is an even multiple of align. Otherwise, it
1206 * would fall on the same boundary every time.
1208 if (rsize & keg->uk_align)
1209 rsize = (rsize & ~keg->uk_align) + alignsize;
1210 if ((rsize & alignsize) == 0)
1212 trailer = rsize - keg->uk_size;
1213 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1214 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1215 keg->uk_rsize = rsize;
1216 keg->uk_ppera = pages;
1217 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1218 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1219 KASSERT(keg->uk_ipers <= uma_max_ipers,
1220 ("keg_small_init: keg->uk_ipers too high(%d) increase max_ipers",
1225 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1226 * the keg onto the global keg list.
1228 * Arguments/Returns follow uma_ctor specifications
1229 * udata Actually uma_kctor_args
1232 keg_ctor(void *mem, int size, void *udata, int flags)
1234 struct uma_kctor_args *arg = udata;
1235 uma_keg_t keg = mem;
1239 keg->uk_size = arg->size;
1240 keg->uk_init = arg->uminit;
1241 keg->uk_fini = arg->fini;
1242 keg->uk_align = arg->align;
1245 keg->uk_flags = arg->flags;
1246 keg->uk_allocf = page_alloc;
1247 keg->uk_freef = page_free;
1248 keg->uk_recurse = 0;
1249 keg->uk_slabzone = NULL;
1252 * The master zone is passed to us at keg-creation time.
1255 keg->uk_name = zone->uz_name;
1257 if (arg->flags & UMA_ZONE_VM)
1258 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1260 if (arg->flags & UMA_ZONE_ZINIT)
1261 keg->uk_init = zero_init;
1263 if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1264 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1267 * The +UMA_FRITM_SZ added to uk_size is to account for the
1268 * linkage that is added to the size in keg_small_init(). If
1269 * we don't account for this here then we may end up in
1270 * keg_small_init() with a calculated 'ipers' of 0.
1272 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1273 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1274 keg_cachespread_init(keg);
1275 else if ((keg->uk_size+UMA_FRITMREF_SZ) >
1276 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1277 keg_large_init(keg);
1279 keg_small_init(keg);
1281 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1282 keg_cachespread_init(keg);
1283 else if ((keg->uk_size+UMA_FRITM_SZ) >
1284 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1285 keg_large_init(keg);
1287 keg_small_init(keg);
1290 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1291 if (keg->uk_flags & UMA_ZONE_REFCNT)
1292 keg->uk_slabzone = slabrefzone;
1294 keg->uk_slabzone = slabzone;
1298 * If we haven't booted yet we need allocations to go through the
1299 * startup cache until the vm is ready.
1301 if (keg->uk_ppera == 1) {
1302 #ifdef UMA_MD_SMALL_ALLOC
1303 keg->uk_allocf = uma_small_alloc;
1304 keg->uk_freef = uma_small_free;
1307 keg->uk_allocf = startup_alloc;
1311 * Initialize keg's lock (shared among zones).
1313 if (arg->flags & UMA_ZONE_MTXCLASS)
1314 KEG_LOCK_INIT(keg, 1);
1316 KEG_LOCK_INIT(keg, 0);
1319 * If we're putting the slab header in the actual page we need to
1320 * figure out where in each page it goes. This calculates a right
1321 * justified offset into the memory on an ALIGN_PTR boundary.
1323 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1326 /* Size of the slab struct and free list */
1327 if (keg->uk_flags & UMA_ZONE_REFCNT)
1328 totsize = sizeof(struct uma_slab_refcnt) +
1329 keg->uk_ipers * UMA_FRITMREF_SZ;
1331 totsize = sizeof(struct uma_slab) +
1332 keg->uk_ipers * UMA_FRITM_SZ;
1334 if (totsize & UMA_ALIGN_PTR)
1335 totsize = (totsize & ~UMA_ALIGN_PTR) +
1336 (UMA_ALIGN_PTR + 1);
1337 keg->uk_pgoff = UMA_SLAB_SIZE - totsize;
1339 if (keg->uk_flags & UMA_ZONE_REFCNT)
1340 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1341 + keg->uk_ipers * UMA_FRITMREF_SZ;
1343 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1344 + keg->uk_ipers * UMA_FRITM_SZ;
1347 * The only way the following is possible is if with our
1348 * UMA_ALIGN_PTR adjustments we are now bigger than
1349 * UMA_SLAB_SIZE. I haven't checked whether this is
1350 * mathematically possible for all cases, so we make
1353 if (totsize > UMA_SLAB_SIZE) {
1354 printf("zone %s ipers %d rsize %d size %d\n",
1355 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1357 panic("UMA slab won't fit.");
1361 if (keg->uk_flags & UMA_ZONE_HASH)
1362 hash_alloc(&keg->uk_hash);
1365 printf("UMA: %s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
1366 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1367 keg->uk_ipers, keg->uk_ppera,
1368 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1371 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1374 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1375 mtx_unlock(&uma_mtx);
1380 * Zone header ctor. This initializes all fields, locks, etc.
1382 * Arguments/Returns follow uma_ctor specifications
1383 * udata Actually uma_zctor_args
1386 zone_ctor(void *mem, int size, void *udata, int flags)
1388 struct uma_zctor_args *arg = udata;
1389 uma_zone_t zone = mem;
1394 zone->uz_name = arg->name;
1395 zone->uz_ctor = arg->ctor;
1396 zone->uz_dtor = arg->dtor;
1397 zone->uz_slab = zone_fetch_slab;
1398 zone->uz_init = NULL;
1399 zone->uz_fini = NULL;
1400 zone->uz_allocs = 0;
1403 zone->uz_fills = zone->uz_count = 0;
1407 if (arg->flags & UMA_ZONE_SECONDARY) {
1408 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1409 zone->uz_init = arg->uminit;
1410 zone->uz_fini = arg->fini;
1411 zone->uz_lock = &keg->uk_lock;
1412 zone->uz_flags |= UMA_ZONE_SECONDARY;
1415 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1416 if (LIST_NEXT(z, uz_link) == NULL) {
1417 LIST_INSERT_AFTER(z, zone, uz_link);
1422 mtx_unlock(&uma_mtx);
1423 } else if (keg == NULL) {
1424 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1425 arg->align, arg->flags)) == NULL)
1428 struct uma_kctor_args karg;
1431 /* We should only be here from uma_startup() */
1432 karg.size = arg->size;
1433 karg.uminit = arg->uminit;
1434 karg.fini = arg->fini;
1435 karg.align = arg->align;
1436 karg.flags = arg->flags;
1438 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1444 * Link in the first keg.
1446 zone->uz_klink.kl_keg = keg;
1447 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1448 zone->uz_lock = &keg->uk_lock;
1449 zone->uz_size = keg->uk_size;
1450 zone->uz_flags |= (keg->uk_flags &
1451 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1454 * Some internal zones don't have room allocated for the per cpu
1455 * caches. If we're internal, bail out here.
1457 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1458 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1459 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1463 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1464 zone->uz_count = BUCKET_MAX;
1465 else if (keg->uk_ipers <= BUCKET_MAX)
1466 zone->uz_count = keg->uk_ipers;
1468 zone->uz_count = BUCKET_MAX;
1473 * Keg header dtor. This frees all data, destroys locks, frees the hash
1474 * table and removes the keg from the global list.
1476 * Arguments/Returns follow uma_dtor specifications
1480 keg_dtor(void *arg, int size, void *udata)
1484 keg = (uma_keg_t)arg;
1486 if (keg->uk_free != 0) {
1487 printf("Freed UMA keg was not empty (%d items). "
1488 " Lost %d pages of memory.\n",
1489 keg->uk_free, keg->uk_pages);
1493 hash_free(&keg->uk_hash);
1501 * Arguments/Returns follow uma_dtor specifications
1505 zone_dtor(void *arg, int size, void *udata)
1511 zone = (uma_zone_t)arg;
1512 keg = zone_first_keg(zone);
1514 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1518 LIST_REMOVE(zone, uz_link);
1519 mtx_unlock(&uma_mtx);
1521 * XXX there are some races here where
1522 * the zone can be drained but zone lock
1523 * released and then refilled before we
1524 * remove it... we dont care for now
1526 zone_drain_wait(zone, M_WAITOK);
1528 * Unlink all of our kegs.
1530 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1531 klink->kl_keg = NULL;
1532 LIST_REMOVE(klink, kl_link);
1533 if (klink == &zone->uz_klink)
1535 free(klink, M_TEMP);
1538 * We only destroy kegs from non secondary zones.
1540 if ((zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1542 LIST_REMOVE(keg, uk_link);
1543 mtx_unlock(&uma_mtx);
1544 zone_free_item(kegs, keg, NULL, SKIP_NONE,
1550 * Traverses every zone in the system and calls a callback
1553 * zfunc A pointer to a function which accepts a zone
1560 zone_foreach(void (*zfunc)(uma_zone_t))
1566 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1567 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1570 mtx_unlock(&uma_mtx);
1573 /* Public functions */
1576 uma_startup(void *bootmem, int boot_pages)
1578 struct uma_zctor_args args;
1581 u_int objsize, totsize, wsize;
1585 printf("Creating uma keg headers zone and keg.\n");
1587 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1590 * Figure out the maximum number of items-per-slab we'll have if
1591 * we're using the OFFPAGE slab header to track free items, given
1592 * all possible object sizes and the maximum desired wastage
1595 * We iterate until we find an object size for
1596 * which the calculated wastage in keg_small_init() will be
1597 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1598 * is an overall increasing see-saw function, we find the smallest
1599 * objsize such that the wastage is always acceptable for objects
1600 * with that objsize or smaller. Since a smaller objsize always
1601 * generates a larger possible uma_max_ipers, we use this computed
1602 * objsize to calculate the largest ipers possible. Since the
1603 * ipers calculated for OFFPAGE slab headers is always larger than
1604 * the ipers initially calculated in keg_small_init(), we use
1605 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1606 * obtain the maximum ipers possible for offpage slab headers.
1608 * It should be noted that ipers versus objsize is an inversly
1609 * proportional function which drops off rather quickly so as
1610 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1611 * falls into the portion of the inverse relation AFTER the steep
1612 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1614 * Note that we have 8-bits (1 byte) to use as a freelist index
1615 * inside the actual slab header itself and this is enough to
1616 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1617 * object with offpage slab header would have ipers =
1618 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1619 * 1 greater than what our byte-integer freelist index can
1620 * accomodate, but we know that this situation never occurs as
1621 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1622 * that we need to go to offpage slab headers. Or, if we do,
1623 * then we trap that condition below and panic in the INVARIANTS case.
1625 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1627 objsize = UMA_SMALLEST_UNIT;
1628 while (totsize >= wsize) {
1629 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1630 (objsize + UMA_FRITM_SZ);
1631 totsize *= (UMA_FRITM_SZ + objsize);
1634 if (objsize > UMA_SMALLEST_UNIT)
1636 uma_max_ipers = MAX(UMA_SLAB_SIZE / objsize, 64);
1638 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1640 objsize = UMA_SMALLEST_UNIT;
1641 while (totsize >= wsize) {
1642 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1643 (objsize + UMA_FRITMREF_SZ);
1644 totsize *= (UMA_FRITMREF_SZ + objsize);
1647 if (objsize > UMA_SMALLEST_UNIT)
1649 uma_max_ipers_ref = MAX(UMA_SLAB_SIZE / objsize, 64);
1651 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1652 ("uma_startup: calculated uma_max_ipers values too large!"));
1655 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1656 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1660 /* "manually" create the initial zone */
1661 args.name = "UMA Kegs";
1662 args.size = sizeof(struct uma_keg);
1663 args.ctor = keg_ctor;
1664 args.dtor = keg_dtor;
1665 args.uminit = zero_init;
1667 args.keg = &masterkeg;
1668 args.align = 32 - 1;
1669 args.flags = UMA_ZFLAG_INTERNAL;
1670 /* The initial zone has no Per cpu queues so it's smaller */
1671 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1674 printf("Filling boot free list.\n");
1676 for (i = 0; i < boot_pages; i++) {
1677 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1678 slab->us_data = (u_int8_t *)slab;
1679 slab->us_flags = UMA_SLAB_BOOT;
1680 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1682 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1685 printf("Creating uma zone headers zone and keg.\n");
1687 args.name = "UMA Zones";
1688 args.size = sizeof(struct uma_zone) +
1689 (sizeof(struct uma_cache) * (mp_maxid + 1));
1690 args.ctor = zone_ctor;
1691 args.dtor = zone_dtor;
1692 args.uminit = zero_init;
1695 args.align = 32 - 1;
1696 args.flags = UMA_ZFLAG_INTERNAL;
1697 /* The initial zone has no Per cpu queues so it's smaller */
1698 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1701 printf("Initializing pcpu cache locks.\n");
1704 printf("Creating slab and hash zones.\n");
1708 * This is the max number of free list items we'll have with
1711 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1712 slabsize += sizeof(struct uma_slab);
1714 /* Now make a zone for slab headers */
1715 slabzone = uma_zcreate("UMA Slabs",
1717 NULL, NULL, NULL, NULL,
1718 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1721 * We also create a zone for the bigger slabs with reference
1722 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1724 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1725 slabsize += sizeof(struct uma_slab_refcnt);
1726 slabrefzone = uma_zcreate("UMA RCntSlabs",
1728 NULL, NULL, NULL, NULL,
1730 UMA_ZFLAG_INTERNAL);
1732 hashzone = uma_zcreate("UMA Hash",
1733 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1734 NULL, NULL, NULL, NULL,
1735 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1739 #if defined(UMA_MD_SMALL_ALLOC) && !defined(UMA_MD_SMALL_ALLOC_NEEDS_VM)
1744 printf("UMA startup complete.\n");
1755 printf("UMA startup2 complete.\n");
1760 * Initialize our callout handle
1768 printf("Starting callout.\n");
1770 callout_init(&uma_callout, CALLOUT_MPSAFE);
1771 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1773 printf("UMA startup3 complete.\n");
1778 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1779 int align, u_int32_t flags)
1781 struct uma_kctor_args args;
1784 args.uminit = uminit;
1786 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1789 return (zone_alloc_item(kegs, &args, M_WAITOK));
1794 uma_set_align(int align)
1797 if (align != UMA_ALIGN_CACHE)
1798 uma_align_cache = align;
1803 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1804 uma_init uminit, uma_fini fini, int align, u_int32_t flags)
1807 struct uma_zctor_args args;
1809 /* This stuff is essential for the zone ctor */
1814 args.uminit = uminit;
1820 return (zone_alloc_item(zones, &args, M_WAITOK));
1825 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1826 uma_init zinit, uma_fini zfini, uma_zone_t master)
1828 struct uma_zctor_args args;
1831 keg = zone_first_keg(master);
1833 args.size = keg->uk_size;
1836 args.uminit = zinit;
1838 args.align = keg->uk_align;
1839 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1842 /* XXX Attaches only one keg of potentially many. */
1843 return (zone_alloc_item(zones, &args, M_WAITOK));
1847 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1851 mtx_lock_flags(b->uz_lock, MTX_DUPOK);
1854 mtx_lock_flags(a->uz_lock, MTX_DUPOK);
1859 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1867 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1874 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1876 zone_lock_pair(zone, master);
1878 * zone must use vtoslab() to resolve objects and must already be
1881 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1882 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1887 * The new master must also use vtoslab().
1889 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1894 * Both must either be refcnt, or not be refcnt.
1896 if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1897 (master->uz_flags & UMA_ZONE_REFCNT)) {
1902 * The underlying object must be the same size. rsize
1905 if (master->uz_size != zone->uz_size) {
1910 * Put it at the end of the list.
1912 klink->kl_keg = zone_first_keg(master);
1913 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1914 if (LIST_NEXT(kl, kl_link) == NULL) {
1915 LIST_INSERT_AFTER(kl, klink, kl_link);
1920 zone->uz_flags |= UMA_ZFLAG_MULTI;
1921 zone->uz_slab = zone_fetch_slab_multi;
1924 zone_unlock_pair(zone, master);
1926 free(klink, M_TEMP);
1934 uma_zdestroy(uma_zone_t zone)
1937 zone_free_item(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1942 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1946 uma_bucket_t bucket;
1949 /* This is the fast path allocation */
1950 #ifdef UMA_DEBUG_ALLOC_1
1951 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1953 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1954 zone->uz_name, flags);
1956 if (flags & M_WAITOK) {
1957 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1958 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1962 * If possible, allocate from the per-CPU cache. There are two
1963 * requirements for safe access to the per-CPU cache: (1) the thread
1964 * accessing the cache must not be preempted or yield during access,
1965 * and (2) the thread must not migrate CPUs without switching which
1966 * cache it accesses. We rely on a critical section to prevent
1967 * preemption and migration. We release the critical section in
1968 * order to acquire the zone mutex if we are unable to allocate from
1969 * the current cache; when we re-acquire the critical section, we
1970 * must detect and handle migration if it has occurred.
1975 cache = &zone->uz_cpu[cpu];
1978 bucket = cache->uc_allocbucket;
1981 if (bucket->ub_cnt > 0) {
1983 item = bucket->ub_bucket[bucket->ub_cnt];
1985 bucket->ub_bucket[bucket->ub_cnt] = NULL;
1987 KASSERT(item != NULL,
1988 ("uma_zalloc: Bucket pointer mangled."));
1993 uma_dbg_alloc(zone, NULL, item);
1996 if (zone->uz_ctor != NULL) {
1997 if (zone->uz_ctor(item, zone->uz_size,
1998 udata, flags) != 0) {
1999 zone_free_item(zone, item, udata,
2000 SKIP_DTOR, ZFREE_STATFAIL |
2006 bzero(item, zone->uz_size);
2008 } else if (cache->uc_freebucket) {
2010 * We have run out of items in our allocbucket.
2011 * See if we can switch with our free bucket.
2013 if (cache->uc_freebucket->ub_cnt > 0) {
2014 #ifdef UMA_DEBUG_ALLOC
2015 printf("uma_zalloc: Swapping empty with"
2018 bucket = cache->uc_freebucket;
2019 cache->uc_freebucket = cache->uc_allocbucket;
2020 cache->uc_allocbucket = bucket;
2027 * Attempt to retrieve the item from the per-CPU cache has failed, so
2028 * we must go back to the zone. This requires the zone lock, so we
2029 * must drop the critical section, then re-acquire it when we go back
2030 * to the cache. Since the critical section is released, we may be
2031 * preempted or migrate. As such, make sure not to maintain any
2032 * thread-local state specific to the cache from prior to releasing
2033 * the critical section.
2039 cache = &zone->uz_cpu[cpu];
2040 bucket = cache->uc_allocbucket;
2041 if (bucket != NULL) {
2042 if (bucket->ub_cnt > 0) {
2046 bucket = cache->uc_freebucket;
2047 if (bucket != NULL && bucket->ub_cnt > 0) {
2053 /* Since we have locked the zone we may as well send back our stats */
2054 zone->uz_allocs += cache->uc_allocs;
2055 cache->uc_allocs = 0;
2056 zone->uz_frees += cache->uc_frees;
2057 cache->uc_frees = 0;
2059 /* Our old one is now a free bucket */
2060 if (cache->uc_allocbucket) {
2061 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
2062 ("uma_zalloc_arg: Freeing a non free bucket."));
2063 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2064 cache->uc_allocbucket, ub_link);
2065 cache->uc_allocbucket = NULL;
2068 /* Check the free list for a new alloc bucket */
2069 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
2070 KASSERT(bucket->ub_cnt != 0,
2071 ("uma_zalloc_arg: Returning an empty bucket."));
2073 LIST_REMOVE(bucket, ub_link);
2074 cache->uc_allocbucket = bucket;
2078 /* We are no longer associated with this CPU. */
2081 /* Bump up our uz_count so we get here less */
2082 if (zone->uz_count < BUCKET_MAX)
2086 * Now lets just fill a bucket and put it on the free list. If that
2087 * works we'll restart the allocation from the begining.
2089 if (zone_alloc_bucket(zone, flags)) {
2091 goto zalloc_restart;
2095 * We may not be able to get a bucket so return an actual item.
2098 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2101 item = zone_alloc_item(zone, udata, flags);
2106 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2110 mtx_assert(&keg->uk_lock, MA_OWNED);
2115 * Find a slab with some space. Prefer slabs that are partially
2116 * used over those that are totally full. This helps to reduce
2119 if (keg->uk_free != 0) {
2120 if (!LIST_EMPTY(&keg->uk_part_slab)) {
2121 slab = LIST_FIRST(&keg->uk_part_slab);
2123 slab = LIST_FIRST(&keg->uk_free_slab);
2124 LIST_REMOVE(slab, us_link);
2125 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2128 MPASS(slab->us_keg == keg);
2133 * M_NOVM means don't ask at all!
2138 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2139 keg->uk_flags |= UMA_ZFLAG_FULL;
2141 * If this is not a multi-zone, set the FULL bit.
2142 * Otherwise slab_multi() takes care of it.
2144 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0)
2145 zone->uz_flags |= UMA_ZFLAG_FULL;
2146 if (flags & M_NOWAIT)
2148 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2152 slab = keg_alloc_slab(keg, zone, flags);
2155 * If we got a slab here it's safe to mark it partially used
2156 * and return. We assume that the caller is going to remove
2157 * at least one item.
2160 MPASS(slab->us_keg == keg);
2161 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2165 * We might not have been able to get a slab but another cpu
2166 * could have while we were unlocked. Check again before we
2175 zone_relock(uma_zone_t zone, uma_keg_t keg)
2177 if (zone->uz_lock != &keg->uk_lock) {
2184 keg_relock(uma_keg_t keg, uma_zone_t zone)
2186 if (zone->uz_lock != &keg->uk_lock) {
2193 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2198 keg = zone_first_keg(zone);
2200 * This is to prevent us from recursively trying to allocate
2201 * buckets. The problem is that if an allocation forces us to
2202 * grab a new bucket we will call page_alloc, which will go off
2203 * and cause the vm to allocate vm_map_entries. If we need new
2204 * buckets there too we will recurse in kmem_alloc and bad
2205 * things happen. So instead we return a NULL bucket, and make
2206 * the code that allocates buckets smart enough to deal with it
2208 if (keg->uk_flags & UMA_ZFLAG_BUCKET && keg->uk_recurse != 0)
2212 slab = keg_fetch_slab(keg, zone, flags);
2215 if (flags & (M_NOWAIT | M_NOVM))
2222 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2223 * with the keg locked. Caller must call zone_relock() afterwards if the
2224 * zone lock is required. On NULL the zone lock is held.
2226 * The last pointer is used to seed the search. It is not required.
2229 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2239 * Don't wait on the first pass. This will skip limit tests
2240 * as well. We don't want to block if we can find a provider
2243 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2245 * Use the last slab allocated as a hint for where to start
2249 slab = keg_fetch_slab(last, zone, flags);
2252 zone_relock(zone, last);
2256 * Loop until we have a slab incase of transient failures
2257 * while M_WAITOK is specified. I'm not sure this is 100%
2258 * required but we've done it for so long now.
2264 * Search the available kegs for slabs. Be careful to hold the
2265 * correct lock while calling into the keg layer.
2267 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2268 keg = klink->kl_keg;
2269 keg_relock(keg, zone);
2270 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2271 slab = keg_fetch_slab(keg, zone, flags);
2275 if (keg->uk_flags & UMA_ZFLAG_FULL)
2279 zone_relock(zone, keg);
2281 if (rflags & (M_NOWAIT | M_NOVM))
2285 * All kegs are full. XXX We can't atomically check all kegs
2286 * and sleep so just sleep for a short period and retry.
2288 if (full && !empty) {
2289 zone->uz_flags |= UMA_ZFLAG_FULL;
2290 msleep(zone, zone->uz_lock, PVM, "zonelimit", hz/100);
2291 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2299 slab_alloc_item(uma_zone_t zone, uma_slab_t slab)
2302 uma_slabrefcnt_t slabref;
2307 mtx_assert(&keg->uk_lock, MA_OWNED);
2309 freei = slab->us_firstfree;
2310 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2311 slabref = (uma_slabrefcnt_t)slab;
2312 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2314 slab->us_firstfree = slab->us_freelist[freei].us_item;
2316 item = slab->us_data + (keg->uk_rsize * freei);
2318 slab->us_freecount--;
2321 uma_dbg_alloc(zone, slab, item);
2323 /* Move this slab to the full list */
2324 if (slab->us_freecount == 0) {
2325 LIST_REMOVE(slab, us_link);
2326 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2333 zone_alloc_bucket(uma_zone_t zone, int flags)
2335 uma_bucket_t bucket;
2339 int max, origflags = flags;
2342 * Try this zone's free list first so we don't allocate extra buckets.
2344 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2345 KASSERT(bucket->ub_cnt == 0,
2346 ("zone_alloc_bucket: Bucket on free list is not empty."));
2347 LIST_REMOVE(bucket, ub_link);
2351 bflags = (flags & ~M_ZERO);
2352 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2356 bucket = bucket_alloc(zone->uz_count, bflags);
2360 if (bucket == NULL) {
2366 * This code is here to limit the number of simultaneous bucket fills
2367 * for any given zone to the number of per cpu caches in this zone. This
2368 * is done so that we don't allocate more memory than we really need.
2370 if (zone->uz_fills >= mp_ncpus)
2376 max = MIN(bucket->ub_entries, zone->uz_count);
2377 /* Try to keep the buckets totally full */
2378 saved = bucket->ub_cnt;
2381 while (bucket->ub_cnt < max &&
2382 (slab = zone->uz_slab(zone, keg, flags)) != NULL) {
2384 while (slab->us_freecount && bucket->ub_cnt < max) {
2385 bucket->ub_bucket[bucket->ub_cnt++] =
2386 slab_alloc_item(zone, slab);
2389 /* Don't block on the next fill */
2393 zone_relock(zone, keg);
2396 * We unlock here because we need to call the zone's init.
2397 * It should be safe to unlock because the slab dealt with
2398 * above is already on the appropriate list within the keg
2399 * and the bucket we filled is not yet on any list, so we
2402 if (zone->uz_init != NULL) {
2406 for (i = saved; i < bucket->ub_cnt; i++)
2407 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2411 * If we couldn't initialize the whole bucket, put the
2412 * rest back onto the freelist.
2414 if (i != bucket->ub_cnt) {
2417 for (j = i; j < bucket->ub_cnt; j++) {
2418 zone_free_item(zone, bucket->ub_bucket[j],
2419 NULL, SKIP_FINI, 0);
2421 bucket->ub_bucket[j] = NULL;
2430 if (bucket->ub_cnt != 0) {
2431 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2438 bucket_free(bucket);
2443 * Allocates an item for an internal zone
2446 * zone The zone to alloc for.
2447 * udata The data to be passed to the constructor.
2448 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2451 * NULL if there is no memory and M_NOWAIT is set
2452 * An item if successful
2456 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2463 #ifdef UMA_DEBUG_ALLOC
2464 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2468 slab = zone->uz_slab(zone, NULL, flags);
2475 item = slab_alloc_item(zone, slab);
2477 zone_relock(zone, slab->us_keg);
2482 * We have to call both the zone's init (not the keg's init)
2483 * and the zone's ctor. This is because the item is going from
2484 * a keg slab directly to the user, and the user is expecting it
2485 * to be both zone-init'd as well as zone-ctor'd.
2487 if (zone->uz_init != NULL) {
2488 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2489 zone_free_item(zone, item, udata, SKIP_FINI,
2490 ZFREE_STATFAIL | ZFREE_STATFREE);
2494 if (zone->uz_ctor != NULL) {
2495 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2496 zone_free_item(zone, item, udata, SKIP_DTOR,
2497 ZFREE_STATFAIL | ZFREE_STATFREE);
2502 bzero(item, zone->uz_size);
2509 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2512 uma_bucket_t bucket;
2516 #ifdef UMA_DEBUG_ALLOC_1
2517 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2519 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2523 zone->uz_dtor(item, zone->uz_size, udata);
2527 if (zone->uz_flags & UMA_ZONE_MALLOC)
2528 uma_dbg_free(zone, udata, item);
2530 uma_dbg_free(zone, NULL, item);
2534 * The race here is acceptable. If we miss it we'll just have to wait
2535 * a little longer for the limits to be reset.
2537 if (zone->uz_flags & UMA_ZFLAG_FULL)
2538 goto zfree_internal;
2541 * If possible, free to the per-CPU cache. There are two
2542 * requirements for safe access to the per-CPU cache: (1) the thread
2543 * accessing the cache must not be preempted or yield during access,
2544 * and (2) the thread must not migrate CPUs without switching which
2545 * cache it accesses. We rely on a critical section to prevent
2546 * preemption and migration. We release the critical section in
2547 * order to acquire the zone mutex if we are unable to free to the
2548 * current cache; when we re-acquire the critical section, we must
2549 * detect and handle migration if it has occurred.
2554 cache = &zone->uz_cpu[cpu];
2557 bucket = cache->uc_freebucket;
2561 * Do we have room in our bucket? It is OK for this uz count
2562 * check to be slightly out of sync.
2565 if (bucket->ub_cnt < bucket->ub_entries) {
2566 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2567 ("uma_zfree: Freeing to non free bucket index."));
2568 bucket->ub_bucket[bucket->ub_cnt] = item;
2573 } else if (cache->uc_allocbucket) {
2574 #ifdef UMA_DEBUG_ALLOC
2575 printf("uma_zfree: Swapping buckets.\n");
2578 * We have run out of space in our freebucket.
2579 * See if we can switch with our alloc bucket.
2581 if (cache->uc_allocbucket->ub_cnt <
2582 cache->uc_freebucket->ub_cnt) {
2583 bucket = cache->uc_freebucket;
2584 cache->uc_freebucket = cache->uc_allocbucket;
2585 cache->uc_allocbucket = bucket;
2591 * We can get here for two reasons:
2593 * 1) The buckets are NULL
2594 * 2) The alloc and free buckets are both somewhat full.
2596 * We must go back the zone, which requires acquiring the zone lock,
2597 * which in turn means we must release and re-acquire the critical
2598 * section. Since the critical section is released, we may be
2599 * preempted or migrate. As such, make sure not to maintain any
2600 * thread-local state specific to the cache from prior to releasing
2601 * the critical section.
2607 cache = &zone->uz_cpu[cpu];
2608 if (cache->uc_freebucket != NULL) {
2609 if (cache->uc_freebucket->ub_cnt <
2610 cache->uc_freebucket->ub_entries) {
2614 if (cache->uc_allocbucket != NULL &&
2615 (cache->uc_allocbucket->ub_cnt <
2616 cache->uc_freebucket->ub_cnt)) {
2622 /* Since we have locked the zone we may as well send back our stats */
2623 zone->uz_allocs += cache->uc_allocs;
2624 cache->uc_allocs = 0;
2625 zone->uz_frees += cache->uc_frees;
2626 cache->uc_frees = 0;
2628 bucket = cache->uc_freebucket;
2629 cache->uc_freebucket = NULL;
2631 /* Can we throw this on the zone full list? */
2632 if (bucket != NULL) {
2633 #ifdef UMA_DEBUG_ALLOC
2634 printf("uma_zfree: Putting old bucket on the free list.\n");
2636 /* ub_cnt is pointing to the last free item */
2637 KASSERT(bucket->ub_cnt != 0,
2638 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2639 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2642 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2643 LIST_REMOVE(bucket, ub_link);
2645 cache->uc_freebucket = bucket;
2648 /* We are no longer associated with this CPU. */
2651 /* And the zone.. */
2654 #ifdef UMA_DEBUG_ALLOC
2655 printf("uma_zfree: Allocating new free bucket.\n");
2659 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2661 bucket = bucket_alloc(zone->uz_count, bflags);
2664 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2671 * If nothing else caught this, we'll just do an internal free.
2674 zone_free_item(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2680 * Frees an item to an INTERNAL zone or allocates a free bucket
2683 * zone The zone to free to
2684 * item The item we're freeing
2685 * udata User supplied data for the dtor
2686 * skip Skip dtors and finis
2689 zone_free_item(uma_zone_t zone, void *item, void *udata,
2690 enum zfreeskip skip, int flags)
2693 uma_slabrefcnt_t slabref;
2699 if (skip < SKIP_DTOR && zone->uz_dtor)
2700 zone->uz_dtor(item, zone->uz_size, udata);
2702 if (skip < SKIP_FINI && zone->uz_fini)
2703 zone->uz_fini(item, zone->uz_size);
2707 if (flags & ZFREE_STATFAIL)
2709 if (flags & ZFREE_STATFREE)
2712 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2713 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2714 keg = zone_first_keg(zone); /* Must only be one. */
2715 if (zone->uz_flags & UMA_ZONE_HASH) {
2716 slab = hash_sfind(&keg->uk_hash, mem);
2718 mem += keg->uk_pgoff;
2719 slab = (uma_slab_t)mem;
2722 /* This prevents redundant lookups via free(). */
2723 if ((zone->uz_flags & UMA_ZONE_MALLOC) && udata != NULL)
2724 slab = (uma_slab_t)udata;
2726 slab = vtoslab((vm_offset_t)item);
2728 keg_relock(keg, zone);
2730 MPASS(keg == slab->us_keg);
2732 /* Do we need to remove from any lists? */
2733 if (slab->us_freecount+1 == keg->uk_ipers) {
2734 LIST_REMOVE(slab, us_link);
2735 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2736 } else if (slab->us_freecount == 0) {
2737 LIST_REMOVE(slab, us_link);
2738 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2741 /* Slab management stuff */
2742 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2747 uma_dbg_free(zone, slab, item);
2750 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2751 slabref = (uma_slabrefcnt_t)slab;
2752 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2754 slab->us_freelist[freei].us_item = slab->us_firstfree;
2756 slab->us_firstfree = freei;
2757 slab->us_freecount++;
2759 /* Zone statistics */
2763 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2764 if (keg->uk_pages < keg->uk_maxpages) {
2765 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2770 * We can handle one more allocation. Since we're clearing ZFLAG_FULL,
2771 * wake up all procs blocked on pages. This should be uncommon, so
2772 * keeping this simple for now (rather than adding count of blocked
2778 zone_relock(zone, keg);
2779 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2788 uma_zone_set_max(uma_zone_t zone, int nitems)
2793 keg = zone_first_keg(zone);
2794 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2795 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2796 keg->uk_maxpages += keg->uk_ppera;
2803 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2808 keg = zone_first_keg(zone);
2809 KASSERT(keg->uk_pages == 0,
2810 ("uma_zone_set_init on non-empty keg"));
2811 keg->uk_init = uminit;
2817 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2822 keg = zone_first_keg(zone);
2823 KASSERT(keg->uk_pages == 0,
2824 ("uma_zone_set_fini on non-empty keg"));
2825 keg->uk_fini = fini;
2831 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2834 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2835 ("uma_zone_set_zinit on non-empty keg"));
2836 zone->uz_init = zinit;
2842 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2845 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2846 ("uma_zone_set_zfini on non-empty keg"));
2847 zone->uz_fini = zfini;
2852 /* XXX uk_freef is not actually used with the zone locked */
2854 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2858 zone_first_keg(zone)->uk_freef = freef;
2863 /* XXX uk_allocf is not actually used with the zone locked */
2865 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2870 keg = zone_first_keg(zone);
2871 keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2872 keg->uk_allocf = allocf;
2878 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2884 keg = zone_first_keg(zone);
2885 pages = count / keg->uk_ipers;
2887 if (pages * keg->uk_ipers < count)
2890 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2895 obj = vm_object_allocate(OBJT_DEFAULT,
2898 VM_OBJECT_LOCK_INIT(obj, "uma object");
2899 _vm_object_allocate(OBJT_DEFAULT,
2905 keg->uk_maxpages = pages;
2906 keg->uk_allocf = obj_alloc;
2907 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2914 uma_prealloc(uma_zone_t zone, int items)
2920 keg = zone_first_keg(zone);
2922 slabs = items / keg->uk_ipers;
2923 if (slabs * keg->uk_ipers < items)
2926 slab = keg_alloc_slab(keg, zone, M_WAITOK);
2929 MPASS(slab->us_keg == keg);
2930 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2938 uma_find_refcnt(uma_zone_t zone, void *item)
2940 uma_slabrefcnt_t slabref;
2945 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
2947 keg = slabref->us_keg;
2948 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
2949 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
2950 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
2952 refcnt = &slabref->us_freelist[idx].us_refcnt;
2961 printf("UMA: vm asked us to release pages!\n");
2964 zone_foreach(zone_drain);
2966 * Some slabs may have been freed but this zone will be visited early
2967 * we visit again so that we can free pages that are empty once other
2968 * zones are drained. We have to do the same for buckets.
2970 zone_drain(slabzone);
2971 zone_drain(slabrefzone);
2972 bucket_zone_drain();
2977 uma_zone_exhausted(uma_zone_t zone)
2982 full = (zone->uz_flags & UMA_ZFLAG_FULL);
2988 uma_zone_exhausted_nolock(uma_zone_t zone)
2990 return (zone->uz_flags & UMA_ZFLAG_FULL);
2994 uma_large_malloc(int size, int wait)
3000 slab = zone_alloc_item(slabzone, NULL, wait);
3003 mem = page_alloc(NULL, size, &flags, wait);
3005 vsetslab((vm_offset_t)mem, slab);
3006 slab->us_data = mem;
3007 slab->us_flags = flags | UMA_SLAB_MALLOC;
3008 slab->us_size = size;
3010 zone_free_item(slabzone, slab, NULL, SKIP_NONE,
3011 ZFREE_STATFAIL | ZFREE_STATFREE);
3018 uma_large_free(uma_slab_t slab)
3020 vsetobj((vm_offset_t)slab->us_data, kmem_object);
3021 page_free(slab->us_data, slab->us_size, slab->us_flags);
3022 zone_free_item(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
3026 uma_print_stats(void)
3028 zone_foreach(uma_print_zone);
3032 slab_print(uma_slab_t slab)
3034 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
3035 slab->us_keg, slab->us_data, slab->us_freecount,
3036 slab->us_firstfree);
3040 cache_print(uma_cache_t cache)
3042 printf("alloc: %p(%d), free: %p(%d)\n",
3043 cache->uc_allocbucket,
3044 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3045 cache->uc_freebucket,
3046 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3050 uma_print_keg(uma_keg_t keg)
3054 printf("keg: %s(%p) size %d(%d) flags %d ipers %d ppera %d "
3055 "out %d free %d limit %d\n",
3056 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3057 keg->uk_ipers, keg->uk_ppera,
3058 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3059 (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3060 printf("Part slabs:\n");
3061 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3063 printf("Free slabs:\n");
3064 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3066 printf("Full slabs:\n");
3067 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3072 uma_print_zone(uma_zone_t zone)
3078 printf("zone: %s(%p) size %d flags %d\n",
3079 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3080 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3081 uma_print_keg(kl->kl_keg);
3082 for (i = 0; i <= mp_maxid; i++) {
3085 cache = &zone->uz_cpu[i];
3086 printf("CPU %d Cache:\n", i);
3093 * Generate statistics across both the zone and its per-cpu cache's. Return
3094 * desired statistics if the pointer is non-NULL for that statistic.
3096 * Note: does not update the zone statistics, as it can't safely clear the
3097 * per-CPU cache statistic.
3099 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3100 * safe from off-CPU; we should modify the caches to track this information
3101 * directly so that we don't have to.
3104 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
3108 u_int64_t allocs, frees;
3113 for (cpu = 0; cpu <= mp_maxid; cpu++) {
3114 if (CPU_ABSENT(cpu))
3116 cache = &z->uz_cpu[cpu];
3117 if (cache->uc_allocbucket != NULL)
3118 cachefree += cache->uc_allocbucket->ub_cnt;
3119 if (cache->uc_freebucket != NULL)
3120 cachefree += cache->uc_freebucket->ub_cnt;
3121 allocs += cache->uc_allocs;
3122 frees += cache->uc_frees;
3124 allocs += z->uz_allocs;
3125 frees += z->uz_frees;
3126 if (cachefreep != NULL)
3127 *cachefreep = cachefree;
3128 if (allocsp != NULL)
3136 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3144 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3145 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3148 mtx_unlock(&uma_mtx);
3149 return (sysctl_handle_int(oidp, &count, 0, req));
3153 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3155 struct uma_stream_header ush;
3156 struct uma_type_header uth;
3157 struct uma_percpu_stat ups;
3158 uma_bucket_t bucket;
3166 int buflen, count, error, i;
3170 mtx_assert(&uma_mtx, MA_OWNED);
3172 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3173 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3176 mtx_unlock(&uma_mtx);
3178 buflen = sizeof(ush) + count * (sizeof(uth) + sizeof(ups) *
3179 (mp_maxid + 1)) + 1;
3180 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
3184 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3185 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3189 free(buffer, M_TEMP);
3194 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
3197 * Insert stream header.
3199 bzero(&ush, sizeof(ush));
3200 ush.ush_version = UMA_STREAM_VERSION;
3201 ush.ush_maxcpus = (mp_maxid + 1);
3202 ush.ush_count = count;
3203 if (sbuf_bcat(&sbuf, &ush, sizeof(ush)) < 0) {
3204 mtx_unlock(&uma_mtx);
3209 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3210 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3211 bzero(&uth, sizeof(uth));
3213 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3214 uth.uth_align = kz->uk_align;
3215 uth.uth_size = kz->uk_size;
3216 uth.uth_rsize = kz->uk_rsize;
3217 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3219 uth.uth_maxpages += k->uk_maxpages;
3220 uth.uth_pages += k->uk_pages;
3221 uth.uth_keg_free += k->uk_free;
3222 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3227 * A zone is secondary is it is not the first entry
3228 * on the keg's zone list.
3230 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3231 (LIST_FIRST(&kz->uk_zones) != z))
3232 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3234 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3235 uth.uth_zone_free += bucket->ub_cnt;
3236 uth.uth_allocs = z->uz_allocs;
3237 uth.uth_frees = z->uz_frees;
3238 uth.uth_fails = z->uz_fails;
3239 if (sbuf_bcat(&sbuf, &uth, sizeof(uth)) < 0) {
3241 mtx_unlock(&uma_mtx);
3246 * While it is not normally safe to access the cache
3247 * bucket pointers while not on the CPU that owns the
3248 * cache, we only allow the pointers to be exchanged
3249 * without the zone lock held, not invalidated, so
3250 * accept the possible race associated with bucket
3251 * exchange during monitoring.
3253 for (i = 0; i < (mp_maxid + 1); i++) {
3254 bzero(&ups, sizeof(ups));
3255 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3259 cache = &z->uz_cpu[i];
3260 if (cache->uc_allocbucket != NULL)
3261 ups.ups_cache_free +=
3262 cache->uc_allocbucket->ub_cnt;
3263 if (cache->uc_freebucket != NULL)
3264 ups.ups_cache_free +=
3265 cache->uc_freebucket->ub_cnt;
3266 ups.ups_allocs = cache->uc_allocs;
3267 ups.ups_frees = cache->uc_frees;
3269 if (sbuf_bcat(&sbuf, &ups, sizeof(ups)) < 0) {
3271 mtx_unlock(&uma_mtx);
3279 mtx_unlock(&uma_mtx);
3281 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
3283 free(buffer, M_TEMP);
3288 DB_SHOW_COMMAND(uma, db_show_uma)
3290 u_int64_t allocs, frees;
3291 uma_bucket_t bucket;
3296 db_printf("%18s %8s %8s %8s %12s\n", "Zone", "Size", "Used", "Free",
3298 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3299 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3300 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3301 allocs = z->uz_allocs;
3302 frees = z->uz_frees;
3305 uma_zone_sumstat(z, &cachefree, &allocs,
3307 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3308 (LIST_FIRST(&kz->uk_zones) != z)))
3309 cachefree += kz->uk_free;
3310 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3311 cachefree += bucket->ub_cnt;
3312 db_printf("%18s %8ju %8jd %8d %12ju\n", z->uz_name,
3313 (uintmax_t)kz->uk_size,
3314 (intmax_t)(allocs - frees), cachefree,