2 * Copyright (c) 2002, 2003, 2004, 2005 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 = 16 - 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 slab_zalloc(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 zone_small_init(uma_zone_t zone);
225 static void zone_large_init(uma_zone_t zone);
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 *uma_zalloc_internal(uma_zone_t, void *, int);
234 static void uma_zfree_internal(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 uma_zalloc_bucket(uma_zone_t zone, int flags);
242 static uma_slab_t uma_zone_slab(uma_zone_t zone, int flags);
243 static void *uma_slab_alloc(uma_zone_t zone, uma_slab_t slab);
244 static uma_zone_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
245 uma_fini fini, int align, u_int32_t flags);
247 void uma_print_zone(uma_zone_t);
248 void uma_print_stats(void);
249 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
250 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
253 static int nosleepwithlocks = 1;
255 static int nosleepwithlocks = 0;
257 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
258 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
259 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
261 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
262 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
264 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
265 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
268 * This routine checks to see whether or not it's safe to enable buckets.
274 if (cnt.v_free_count < cnt.v_free_min)
281 * Initialize bucket_zones, the array of zones of buckets of various sizes.
283 * For each zone, calculate the memory required for each bucket, consisting
284 * of the header and an array of pointers. Initialize bucket_size[] to point
285 * the range of appropriate bucket sizes at the zone.
290 struct uma_bucket_zone *ubz;
294 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
297 ubz = &bucket_zones[j];
298 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
299 size += sizeof(void *) * ubz->ubz_entries;
300 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
301 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
302 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
303 bucket_size[i >> BUCKET_SHIFT] = j;
308 * Given a desired number of entries for a bucket, return the zone from which
309 * to allocate the bucket.
311 static struct uma_bucket_zone *
312 bucket_zone_lookup(int entries)
316 idx = howmany(entries, 1 << BUCKET_SHIFT);
317 return (&bucket_zones[bucket_size[idx]]);
321 bucket_alloc(int entries, int bflags)
323 struct uma_bucket_zone *ubz;
327 * This is to stop us from allocating per cpu buckets while we're
328 * running out of vm.boot_pages. Otherwise, we would exhaust the
329 * boot pages. This also prevents us from allocating buckets in
330 * low memory situations.
335 ubz = bucket_zone_lookup(entries);
336 bucket = uma_zalloc_internal(ubz->ubz_zone, NULL, bflags);
339 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
342 bucket->ub_entries = ubz->ubz_entries;
349 bucket_free(uma_bucket_t bucket)
351 struct uma_bucket_zone *ubz;
353 ubz = bucket_zone_lookup(bucket->ub_entries);
354 uma_zfree_internal(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
359 bucket_zone_drain(void)
361 struct uma_bucket_zone *ubz;
363 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
364 zone_drain(ubz->ubz_zone);
369 * Routine called by timeout which is used to fire off some time interval
370 * based calculations. (stats, hash size, etc.)
379 uma_timeout(void *unused)
382 zone_foreach(zone_timeout);
384 /* Reschedule this event */
385 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
389 * Routine to perform timeout driven calculations. This expands the
390 * hashes and does per cpu statistics aggregation.
393 * zone The zone to operate on
399 zone_timeout(uma_zone_t zone)
408 * Expand the zone hash table.
410 * This is done if the number of slabs is larger than the hash size.
411 * What I'm trying to do here is completely reduce collisions. This
412 * may be a little aggressive. Should I allow for two collisions max?
415 if (keg->uk_flags & UMA_ZONE_HASH &&
416 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
417 struct uma_hash newhash;
418 struct uma_hash oldhash;
422 * This is so involved because allocating and freeing
423 * while the zone lock is held will lead to deadlock.
424 * I have to do everything in stages and check for
427 newhash = keg->uk_hash;
429 ret = hash_alloc(&newhash);
432 if (hash_expand(&keg->uk_hash, &newhash)) {
433 oldhash = keg->uk_hash;
434 keg->uk_hash = newhash;
447 * Allocate and zero fill the next sized hash table from the appropriate
451 * hash A new hash structure with the old hash size in uh_hashsize
454 * 1 on sucess and 0 on failure.
457 hash_alloc(struct uma_hash *hash)
462 oldsize = hash->uh_hashsize;
464 /* We're just going to go to a power of two greater */
466 hash->uh_hashsize = oldsize * 2;
467 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
468 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
469 M_UMAHASH, M_NOWAIT);
471 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
472 hash->uh_slab_hash = uma_zalloc_internal(hashzone, NULL,
474 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
476 if (hash->uh_slab_hash) {
477 bzero(hash->uh_slab_hash, alloc);
478 hash->uh_hashmask = hash->uh_hashsize - 1;
486 * Expands the hash table for HASH zones. This is done from zone_timeout
487 * to reduce collisions. This must not be done in the regular allocation
488 * path, otherwise, we can recurse on the vm while allocating pages.
491 * oldhash The hash you want to expand
492 * newhash The hash structure for the new table
500 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
506 if (!newhash->uh_slab_hash)
509 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
513 * I need to investigate hash algorithms for resizing without a
517 for (i = 0; i < oldhash->uh_hashsize; i++)
518 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
519 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
520 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
521 hval = UMA_HASH(newhash, slab->us_data);
522 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
530 * Free the hash bucket to the appropriate backing store.
533 * slab_hash The hash bucket we're freeing
534 * hashsize The number of entries in that hash bucket
540 hash_free(struct uma_hash *hash)
542 if (hash->uh_slab_hash == NULL)
544 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
545 uma_zfree_internal(hashzone,
546 hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
548 free(hash->uh_slab_hash, M_UMAHASH);
552 * Frees all outstanding items in a bucket
555 * zone The zone to free to, must be unlocked.
556 * bucket The free/alloc bucket with items, cpu queue must be locked.
563 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
575 /* We have to lookup the slab again for malloc.. */
576 if (zone->uz_keg->uk_flags & UMA_ZONE_MALLOC)
579 while (bucket->ub_cnt > 0) {
581 item = bucket->ub_bucket[bucket->ub_cnt];
583 bucket->ub_bucket[bucket->ub_cnt] = NULL;
584 KASSERT(item != NULL,
585 ("bucket_drain: botched ptr, item is NULL"));
588 * This is extremely inefficient. The slab pointer was passed
589 * to uma_zfree_arg, but we lost it because the buckets don't
590 * hold them. This will go away when free() gets a size passed
594 slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
595 uma_zfree_internal(zone, item, slab, SKIP_DTOR, 0);
600 * Drains the per cpu caches for a zone.
602 * NOTE: This may only be called while the zone is being turn down, and not
603 * during normal operation. This is necessary in order that we do not have
604 * to migrate CPUs to drain the per-CPU caches.
607 * zone The zone to drain, must be unlocked.
613 cache_drain(uma_zone_t zone)
619 * XXX: It is safe to not lock the per-CPU caches, because we're
620 * tearing down the zone anyway. I.e., there will be no further use
621 * of the caches at this point.
623 * XXX: It would good to be able to assert that the zone is being
624 * torn down to prevent improper use of cache_drain().
626 * XXX: We lock the zone before passing into bucket_cache_drain() as
627 * it is used elsewhere. Should the tear-down path be made special
628 * there in some form?
630 for (cpu = 0; cpu <= mp_maxid; cpu++) {
633 cache = &zone->uz_cpu[cpu];
634 bucket_drain(zone, cache->uc_allocbucket);
635 bucket_drain(zone, cache->uc_freebucket);
636 if (cache->uc_allocbucket != NULL)
637 bucket_free(cache->uc_allocbucket);
638 if (cache->uc_freebucket != NULL)
639 bucket_free(cache->uc_freebucket);
640 cache->uc_allocbucket = cache->uc_freebucket = NULL;
643 bucket_cache_drain(zone);
648 * Drain the cached buckets from a zone. Expects a locked zone on entry.
651 bucket_cache_drain(uma_zone_t zone)
656 * Drain the bucket queues and free the buckets, we just keep two per
659 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
660 LIST_REMOVE(bucket, ub_link);
662 bucket_drain(zone, bucket);
667 /* Now we do the free queue.. */
668 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
669 LIST_REMOVE(bucket, ub_link);
675 * Frees pages from a zone back to the system. This is done on demand from
676 * the pageout daemon.
679 * zone The zone to free pages from
680 * all Should we drain all items?
686 zone_drain(uma_zone_t zone)
688 struct slabhead freeslabs = { 0 };
699 * We don't want to take pages from statically allocated zones at this
702 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
708 printf("%s free items: %u\n", zone->uz_name, keg->uk_free);
710 bucket_cache_drain(zone);
711 if (keg->uk_free == 0)
714 slab = LIST_FIRST(&keg->uk_free_slab);
716 n = LIST_NEXT(slab, us_link);
718 /* We have no where to free these to */
719 if (slab->us_flags & UMA_SLAB_BOOT) {
724 LIST_REMOVE(slab, us_link);
725 keg->uk_pages -= keg->uk_ppera;
726 keg->uk_free -= keg->uk_ipers;
728 if (keg->uk_flags & UMA_ZONE_HASH)
729 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
731 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
738 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
739 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
741 for (i = 0; i < keg->uk_ipers; i++)
743 slab->us_data + (keg->uk_rsize * i),
745 flags = slab->us_flags;
748 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
749 (keg->uk_flags & UMA_ZONE_REFCNT)) {
752 if (flags & UMA_SLAB_KMEM)
756 for (i = 0; i < keg->uk_ppera; i++)
757 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
760 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
761 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
762 SKIP_NONE, ZFREE_STATFREE);
764 printf("%s: Returning %d bytes.\n",
765 zone->uz_name, UMA_SLAB_SIZE * keg->uk_ppera);
767 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
772 * Allocate a new slab for a zone. This does not insert the slab onto a list.
775 * zone The zone to allocate slabs for
776 * wait Shall we wait?
779 * The slab that was allocated or NULL if there is no memory and the
780 * caller specified M_NOWAIT.
783 slab_zalloc(uma_zone_t zone, int wait)
785 uma_slabrefcnt_t slabref;
796 printf("slab_zalloc: Allocating a new slab for %s\n", zone->uz_name);
800 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
801 slab = uma_zalloc_internal(keg->uk_slabzone, NULL, wait);
809 * This reproduces the old vm_zone behavior of zero filling pages the
810 * first time they are added to a zone.
812 * Malloced items are zeroed in uma_zalloc.
815 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
820 mem = keg->uk_allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE,
823 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
824 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
825 SKIP_NONE, ZFREE_STATFREE);
830 /* Point the slab into the allocated memory */
831 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
832 slab = (uma_slab_t )(mem + keg->uk_pgoff);
834 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
835 (keg->uk_flags & UMA_ZONE_REFCNT))
836 for (i = 0; i < keg->uk_ppera; i++)
837 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
841 slab->us_freecount = keg->uk_ipers;
842 slab->us_firstfree = 0;
843 slab->us_flags = flags;
845 if (keg->uk_flags & UMA_ZONE_REFCNT) {
846 slabref = (uma_slabrefcnt_t)slab;
847 for (i = 0; i < keg->uk_ipers; i++) {
848 slabref->us_freelist[i].us_refcnt = 0;
849 slabref->us_freelist[i].us_item = i+1;
852 for (i = 0; i < keg->uk_ipers; i++)
853 slab->us_freelist[i].us_item = i+1;
856 if (keg->uk_init != NULL) {
857 for (i = 0; i < keg->uk_ipers; i++)
858 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
859 keg->uk_size, wait) != 0)
861 if (i != keg->uk_ipers) {
862 if (keg->uk_fini != NULL) {
863 for (i--; i > -1; i--)
864 keg->uk_fini(slab->us_data +
868 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
869 (keg->uk_flags & UMA_ZONE_REFCNT)) {
872 if (flags & UMA_SLAB_KMEM)
876 for (i = 0; i < keg->uk_ppera; i++)
877 vsetobj((vm_offset_t)mem +
878 (i * PAGE_SIZE), obj);
880 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
881 uma_zfree_internal(keg->uk_slabzone, slab,
882 NULL, SKIP_NONE, ZFREE_STATFREE);
883 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
891 if (keg->uk_flags & UMA_ZONE_HASH)
892 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
894 keg->uk_pages += keg->uk_ppera;
895 keg->uk_free += keg->uk_ipers;
901 * This function is intended to be used early on in place of page_alloc() so
902 * that we may use the boot time page cache to satisfy allocations before
906 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
914 * Check our small startup cache to see if it has pages remaining.
916 mtx_lock(&uma_boot_pages_mtx);
917 if ((tmps = LIST_FIRST(&uma_boot_pages)) != NULL) {
918 LIST_REMOVE(tmps, us_link);
919 mtx_unlock(&uma_boot_pages_mtx);
920 *pflag = tmps->us_flags;
921 return (tmps->us_data);
923 mtx_unlock(&uma_boot_pages_mtx);
925 panic("UMA: Increase vm.boot_pages");
927 * Now that we've booted reset these users to their real allocator.
929 #ifdef UMA_MD_SMALL_ALLOC
930 keg->uk_allocf = uma_small_alloc;
932 keg->uk_allocf = page_alloc;
934 return keg->uk_allocf(zone, bytes, pflag, wait);
938 * Allocates a number of pages from the system
942 * bytes The number of bytes requested
943 * wait Shall we wait?
946 * A pointer to the alloced memory or possibly
947 * NULL if M_NOWAIT is set.
950 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
952 void *p; /* Returned page */
954 *pflag = UMA_SLAB_KMEM;
955 p = (void *) kmem_malloc(kmem_map, bytes, wait);
961 * Allocates a number of pages from within an object
965 * bytes The number of bytes requested
966 * wait Shall we wait?
969 * A pointer to the alloced memory or possibly
970 * NULL if M_NOWAIT is set.
973 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
976 vm_offset_t retkva, zkva;
978 int pages, startpages;
980 object = zone->uz_keg->uk_obj;
984 * This looks a little weird since we're getting one page at a time.
986 VM_OBJECT_LOCK(object);
987 p = TAILQ_LAST(&object->memq, pglist);
988 pages = p != NULL ? p->pindex + 1 : 0;
990 zkva = zone->uz_keg->uk_kva + pages * PAGE_SIZE;
991 for (; bytes > 0; bytes -= PAGE_SIZE) {
992 p = vm_page_alloc(object, pages,
993 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
995 if (pages != startpages)
996 pmap_qremove(retkva, pages - startpages);
997 while (pages != startpages) {
999 p = TAILQ_LAST(&object->memq, pglist);
1000 vm_page_lock_queues();
1001 vm_page_unwire(p, 0);
1003 vm_page_unlock_queues();
1008 pmap_qenter(zkva, &p, 1);
1015 VM_OBJECT_UNLOCK(object);
1016 *flags = UMA_SLAB_PRIV;
1018 return ((void *)retkva);
1022 * Frees a number of pages to the system
1025 * mem A pointer to the memory to be freed
1026 * size The size of the memory being freed
1027 * flags The original p->us_flags field
1033 page_free(void *mem, int size, u_int8_t flags)
1037 if (flags & UMA_SLAB_KMEM)
1040 panic("UMA: page_free used with invalid flags %d\n", flags);
1042 kmem_free(map, (vm_offset_t)mem, size);
1046 * Zero fill initializer
1048 * Arguments/Returns follow uma_init specifications
1051 zero_init(void *mem, int size, int flags)
1058 * Finish creating a small uma zone. This calculates ipers, and the zone size.
1061 * zone The zone we should initialize
1067 zone_small_init(uma_zone_t zone)
1076 KASSERT(keg != NULL, ("Keg is null in zone_small_init"));
1077 rsize = keg->uk_size;
1079 if (rsize < UMA_SMALLEST_UNIT)
1080 rsize = UMA_SMALLEST_UNIT;
1081 if (rsize & keg->uk_align)
1082 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1084 keg->uk_rsize = rsize;
1087 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1088 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1089 shsize = sizeof(struct uma_slab_refcnt);
1091 rsize += UMA_FRITM_SZ; /* Account for linkage */
1092 shsize = sizeof(struct uma_slab);
1095 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1096 KASSERT(keg->uk_ipers != 0, ("zone_small_init: ipers is 0"));
1097 memused = keg->uk_ipers * rsize + shsize;
1098 wastedspace = UMA_SLAB_SIZE - memused;
1101 * We can't do OFFPAGE if we're internal or if we've been
1102 * asked to not go to the VM for buckets. If we do this we
1103 * may end up going to the VM (kmem_map) for slabs which we
1104 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1105 * result of UMA_ZONE_VM, which clearly forbids it.
1107 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1108 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1111 if ((wastedspace >= UMA_MAX_WASTE) &&
1112 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1113 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1114 KASSERT(keg->uk_ipers <= 255,
1115 ("zone_small_init: keg->uk_ipers too high!"));
1117 printf("UMA decided we need offpage slab headers for "
1118 "zone: %s, calculated wastedspace = %d, "
1119 "maximum wasted space allowed = %d, "
1120 "calculated ipers = %d, "
1121 "new wasted space = %d\n", zone->uz_name, wastedspace,
1122 UMA_MAX_WASTE, keg->uk_ipers,
1123 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1125 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1126 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1127 keg->uk_flags |= UMA_ZONE_HASH;
1132 * Finish creating a large (> UMA_SLAB_SIZE) uma zone. Just give in and do
1133 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1137 * zone The zone we should initialize
1143 zone_large_init(uma_zone_t zone)
1150 KASSERT(keg != NULL, ("Keg is null in zone_large_init"));
1151 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1152 ("zone_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY zone"));
1154 pages = keg->uk_size / UMA_SLAB_SIZE;
1156 /* Account for remainder */
1157 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1160 keg->uk_ppera = pages;
1163 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1164 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1165 keg->uk_flags |= UMA_ZONE_HASH;
1167 keg->uk_rsize = keg->uk_size;
1171 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1172 * the keg onto the global keg list.
1174 * Arguments/Returns follow uma_ctor specifications
1175 * udata Actually uma_kctor_args
1178 keg_ctor(void *mem, int size, void *udata, int flags)
1180 struct uma_kctor_args *arg = udata;
1181 uma_keg_t keg = mem;
1185 keg->uk_size = arg->size;
1186 keg->uk_init = arg->uminit;
1187 keg->uk_fini = arg->fini;
1188 keg->uk_align = arg->align;
1191 keg->uk_flags = arg->flags;
1192 keg->uk_allocf = page_alloc;
1193 keg->uk_freef = page_free;
1194 keg->uk_recurse = 0;
1195 keg->uk_slabzone = NULL;
1198 * The master zone is passed to us at keg-creation time.
1203 if (arg->flags & UMA_ZONE_VM)
1204 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1206 if (arg->flags & UMA_ZONE_ZINIT)
1207 keg->uk_init = zero_init;
1210 * The +UMA_FRITM_SZ added to uk_size is to account for the
1211 * linkage that is added to the size in zone_small_init(). If
1212 * we don't account for this here then we may end up in
1213 * zone_small_init() with a calculated 'ipers' of 0.
1215 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1216 if ((keg->uk_size+UMA_FRITMREF_SZ) >
1217 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1218 zone_large_init(zone);
1220 zone_small_init(zone);
1222 if ((keg->uk_size+UMA_FRITM_SZ) >
1223 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1224 zone_large_init(zone);
1226 zone_small_init(zone);
1229 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1230 if (keg->uk_flags & UMA_ZONE_REFCNT)
1231 keg->uk_slabzone = slabrefzone;
1233 keg->uk_slabzone = slabzone;
1237 * If we haven't booted yet we need allocations to go through the
1238 * startup cache until the vm is ready.
1240 if (keg->uk_ppera == 1) {
1241 #ifdef UMA_MD_SMALL_ALLOC
1242 keg->uk_allocf = uma_small_alloc;
1243 keg->uk_freef = uma_small_free;
1246 keg->uk_allocf = startup_alloc;
1250 * Initialize keg's lock (shared among zones) through
1253 zone->uz_lock = &keg->uk_lock;
1254 if (arg->flags & UMA_ZONE_MTXCLASS)
1255 ZONE_LOCK_INIT(zone, 1);
1257 ZONE_LOCK_INIT(zone, 0);
1260 * If we're putting the slab header in the actual page we need to
1261 * figure out where in each page it goes. This calculates a right
1262 * justified offset into the memory on an ALIGN_PTR boundary.
1264 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1267 /* Size of the slab struct and free list */
1268 if (keg->uk_flags & UMA_ZONE_REFCNT)
1269 totsize = sizeof(struct uma_slab_refcnt) +
1270 keg->uk_ipers * UMA_FRITMREF_SZ;
1272 totsize = sizeof(struct uma_slab) +
1273 keg->uk_ipers * UMA_FRITM_SZ;
1275 if (totsize & UMA_ALIGN_PTR)
1276 totsize = (totsize & ~UMA_ALIGN_PTR) +
1277 (UMA_ALIGN_PTR + 1);
1278 keg->uk_pgoff = UMA_SLAB_SIZE - totsize;
1280 if (keg->uk_flags & UMA_ZONE_REFCNT)
1281 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1282 + keg->uk_ipers * UMA_FRITMREF_SZ;
1284 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1285 + keg->uk_ipers * UMA_FRITM_SZ;
1288 * The only way the following is possible is if with our
1289 * UMA_ALIGN_PTR adjustments we are now bigger than
1290 * UMA_SLAB_SIZE. I haven't checked whether this is
1291 * mathematically possible for all cases, so we make
1294 if (totsize > UMA_SLAB_SIZE) {
1295 printf("zone %s ipers %d rsize %d size %d\n",
1296 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1298 panic("UMA slab won't fit.\n");
1302 if (keg->uk_flags & UMA_ZONE_HASH)
1303 hash_alloc(&keg->uk_hash);
1306 printf("%s(%p) size = %d ipers = %d ppera = %d pgoff = %d\n",
1307 zone->uz_name, zone,
1308 keg->uk_size, keg->uk_ipers,
1309 keg->uk_ppera, keg->uk_pgoff);
1312 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1315 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1316 mtx_unlock(&uma_mtx);
1321 * Zone header ctor. This initializes all fields, locks, etc.
1323 * Arguments/Returns follow uma_ctor specifications
1324 * udata Actually uma_zctor_args
1328 zone_ctor(void *mem, int size, void *udata, int flags)
1330 struct uma_zctor_args *arg = udata;
1331 uma_zone_t zone = mem;
1336 zone->uz_name = arg->name;
1337 zone->uz_ctor = arg->ctor;
1338 zone->uz_dtor = arg->dtor;
1339 zone->uz_init = NULL;
1340 zone->uz_fini = NULL;
1341 zone->uz_allocs = 0;
1344 zone->uz_fills = zone->uz_count = 0;
1346 if (arg->flags & UMA_ZONE_SECONDARY) {
1347 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1350 zone->uz_init = arg->uminit;
1351 zone->uz_fini = arg->fini;
1352 zone->uz_lock = &keg->uk_lock;
1355 keg->uk_flags |= UMA_ZONE_SECONDARY;
1356 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1357 if (LIST_NEXT(z, uz_link) == NULL) {
1358 LIST_INSERT_AFTER(z, zone, uz_link);
1363 mtx_unlock(&uma_mtx);
1364 } else if (arg->keg == NULL) {
1365 if (uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1366 arg->align, arg->flags) == NULL)
1369 struct uma_kctor_args karg;
1372 /* We should only be here from uma_startup() */
1373 karg.size = arg->size;
1374 karg.uminit = arg->uminit;
1375 karg.fini = arg->fini;
1376 karg.align = arg->align;
1377 karg.flags = arg->flags;
1379 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1385 zone->uz_lock = &keg->uk_lock;
1388 * Some internal zones don't have room allocated for the per cpu
1389 * caches. If we're internal, bail out here.
1391 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1392 KASSERT((keg->uk_flags & UMA_ZONE_SECONDARY) == 0,
1393 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1397 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1398 zone->uz_count = BUCKET_MAX;
1399 else if (keg->uk_ipers <= BUCKET_MAX)
1400 zone->uz_count = keg->uk_ipers;
1402 zone->uz_count = BUCKET_MAX;
1407 * Keg header dtor. This frees all data, destroys locks, frees the hash
1408 * table and removes the keg from the global list.
1410 * Arguments/Returns follow uma_dtor specifications
1414 keg_dtor(void *arg, int size, void *udata)
1418 keg = (uma_keg_t)arg;
1419 mtx_lock(&keg->uk_lock);
1420 if (keg->uk_free != 0) {
1421 printf("Freed UMA keg was not empty (%d items). "
1422 " Lost %d pages of memory.\n",
1423 keg->uk_free, keg->uk_pages);
1425 mtx_unlock(&keg->uk_lock);
1427 if (keg->uk_flags & UMA_ZONE_HASH)
1428 hash_free(&keg->uk_hash);
1430 mtx_destroy(&keg->uk_lock);
1436 * Arguments/Returns follow uma_dtor specifications
1440 zone_dtor(void *arg, int size, void *udata)
1445 zone = (uma_zone_t)arg;
1448 if (!(keg->uk_flags & UMA_ZFLAG_INTERNAL))
1453 if (keg->uk_flags & UMA_ZONE_SECONDARY) {
1454 LIST_REMOVE(zone, uz_link);
1456 * XXX there are some races here where
1457 * the zone can be drained but zone lock
1458 * released and then refilled before we
1459 * remove it... we dont care for now
1462 if (LIST_EMPTY(&keg->uk_zones))
1463 keg->uk_flags &= ~UMA_ZONE_SECONDARY;
1465 mtx_unlock(&uma_mtx);
1467 LIST_REMOVE(keg, uk_link);
1468 LIST_REMOVE(zone, uz_link);
1469 mtx_unlock(&uma_mtx);
1470 uma_zfree_internal(kegs, keg, NULL, SKIP_NONE,
1473 zone->uz_keg = NULL;
1477 * Traverses every zone in the system and calls a callback
1480 * zfunc A pointer to a function which accepts a zone
1487 zone_foreach(void (*zfunc)(uma_zone_t))
1493 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1494 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1497 mtx_unlock(&uma_mtx);
1500 /* Public functions */
1503 uma_startup(void *bootmem, int boot_pages)
1505 struct uma_zctor_args args;
1508 u_int objsize, totsize, wsize;
1512 printf("Creating uma keg headers zone and keg.\n");
1514 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1517 * Figure out the maximum number of items-per-slab we'll have if
1518 * we're using the OFFPAGE slab header to track free items, given
1519 * all possible object sizes and the maximum desired wastage
1522 * We iterate until we find an object size for
1523 * which the calculated wastage in zone_small_init() will be
1524 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1525 * is an overall increasing see-saw function, we find the smallest
1526 * objsize such that the wastage is always acceptable for objects
1527 * with that objsize or smaller. Since a smaller objsize always
1528 * generates a larger possible uma_max_ipers, we use this computed
1529 * objsize to calculate the largest ipers possible. Since the
1530 * ipers calculated for OFFPAGE slab headers is always larger than
1531 * the ipers initially calculated in zone_small_init(), we use
1532 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1533 * obtain the maximum ipers possible for offpage slab headers.
1535 * It should be noted that ipers versus objsize is an inversly
1536 * proportional function which drops off rather quickly so as
1537 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1538 * falls into the portion of the inverse relation AFTER the steep
1539 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1541 * Note that we have 8-bits (1 byte) to use as a freelist index
1542 * inside the actual slab header itself and this is enough to
1543 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1544 * object with offpage slab header would have ipers =
1545 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1546 * 1 greater than what our byte-integer freelist index can
1547 * accomodate, but we know that this situation never occurs as
1548 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1549 * that we need to go to offpage slab headers. Or, if we do,
1550 * then we trap that condition below and panic in the INVARIANTS case.
1552 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1554 objsize = UMA_SMALLEST_UNIT;
1555 while (totsize >= wsize) {
1556 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1557 (objsize + UMA_FRITM_SZ);
1558 totsize *= (UMA_FRITM_SZ + objsize);
1561 if (objsize > UMA_SMALLEST_UNIT)
1563 uma_max_ipers = UMA_SLAB_SIZE / objsize;
1565 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1567 objsize = UMA_SMALLEST_UNIT;
1568 while (totsize >= wsize) {
1569 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1570 (objsize + UMA_FRITMREF_SZ);
1571 totsize *= (UMA_FRITMREF_SZ + objsize);
1574 if (objsize > UMA_SMALLEST_UNIT)
1576 uma_max_ipers_ref = UMA_SLAB_SIZE / objsize;
1578 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1579 ("uma_startup: calculated uma_max_ipers values too large!"));
1582 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1583 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1587 /* "manually" create the initial zone */
1588 args.name = "UMA Kegs";
1589 args.size = sizeof(struct uma_keg);
1590 args.ctor = keg_ctor;
1591 args.dtor = keg_dtor;
1592 args.uminit = zero_init;
1594 args.keg = &masterkeg;
1595 args.align = 32 - 1;
1596 args.flags = UMA_ZFLAG_INTERNAL;
1597 /* The initial zone has no Per cpu queues so it's smaller */
1598 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1601 printf("Filling boot free list.\n");
1603 for (i = 0; i < boot_pages; i++) {
1604 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1605 slab->us_data = (u_int8_t *)slab;
1606 slab->us_flags = UMA_SLAB_BOOT;
1607 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1609 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1612 printf("Creating uma zone headers zone and keg.\n");
1614 args.name = "UMA Zones";
1615 args.size = sizeof(struct uma_zone) +
1616 (sizeof(struct uma_cache) * (mp_maxid + 1));
1617 args.ctor = zone_ctor;
1618 args.dtor = zone_dtor;
1619 args.uminit = zero_init;
1622 args.align = 32 - 1;
1623 args.flags = UMA_ZFLAG_INTERNAL;
1624 /* The initial zone has no Per cpu queues so it's smaller */
1625 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1628 printf("Initializing pcpu cache locks.\n");
1631 printf("Creating slab and hash zones.\n");
1635 * This is the max number of free list items we'll have with
1638 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1639 slabsize += sizeof(struct uma_slab);
1641 /* Now make a zone for slab headers */
1642 slabzone = uma_zcreate("UMA Slabs",
1644 NULL, NULL, NULL, NULL,
1645 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1648 * We also create a zone for the bigger slabs with reference
1649 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1651 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1652 slabsize += sizeof(struct uma_slab_refcnt);
1653 slabrefzone = uma_zcreate("UMA RCntSlabs",
1655 NULL, NULL, NULL, NULL,
1657 UMA_ZFLAG_INTERNAL);
1659 hashzone = uma_zcreate("UMA Hash",
1660 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1661 NULL, NULL, NULL, NULL,
1662 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1666 #ifdef UMA_MD_SMALL_ALLOC
1671 printf("UMA startup complete.\n");
1682 printf("UMA startup2 complete.\n");
1687 * Initialize our callout handle
1695 printf("Starting callout.\n");
1697 callout_init(&uma_callout, CALLOUT_MPSAFE);
1698 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1700 printf("UMA startup3 complete.\n");
1705 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1706 int align, u_int32_t flags)
1708 struct uma_kctor_args args;
1711 args.uminit = uminit;
1713 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1716 return (uma_zalloc_internal(kegs, &args, M_WAITOK));
1721 uma_set_align(int align)
1724 if (align != UMA_ALIGN_CACHE)
1725 uma_align_cache = align;
1730 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1731 uma_init uminit, uma_fini fini, int align, u_int32_t flags)
1734 struct uma_zctor_args args;
1736 /* This stuff is essential for the zone ctor */
1741 args.uminit = uminit;
1747 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1752 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1753 uma_init zinit, uma_fini zfini, uma_zone_t master)
1755 struct uma_zctor_args args;
1758 args.size = master->uz_keg->uk_size;
1761 args.uminit = zinit;
1763 args.align = master->uz_keg->uk_align;
1764 args.flags = master->uz_keg->uk_flags | UMA_ZONE_SECONDARY;
1765 args.keg = master->uz_keg;
1767 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1772 uma_zdestroy(uma_zone_t zone)
1775 uma_zfree_internal(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1780 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1784 uma_bucket_t bucket;
1787 /* This is the fast path allocation */
1788 #ifdef UMA_DEBUG_ALLOC_1
1789 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1791 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1792 zone->uz_name, flags);
1794 if (flags & M_WAITOK) {
1795 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1796 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1800 * If possible, allocate from the per-CPU cache. There are two
1801 * requirements for safe access to the per-CPU cache: (1) the thread
1802 * accessing the cache must not be preempted or yield during access,
1803 * and (2) the thread must not migrate CPUs without switching which
1804 * cache it accesses. We rely on a critical section to prevent
1805 * preemption and migration. We release the critical section in
1806 * order to acquire the zone mutex if we are unable to allocate from
1807 * the current cache; when we re-acquire the critical section, we
1808 * must detect and handle migration if it has occurred.
1813 cache = &zone->uz_cpu[cpu];
1816 bucket = cache->uc_allocbucket;
1819 if (bucket->ub_cnt > 0) {
1821 item = bucket->ub_bucket[bucket->ub_cnt];
1823 bucket->ub_bucket[bucket->ub_cnt] = NULL;
1825 KASSERT(item != NULL,
1826 ("uma_zalloc: Bucket pointer mangled."));
1831 uma_dbg_alloc(zone, NULL, item);
1834 if (zone->uz_ctor != NULL) {
1835 if (zone->uz_ctor(item, zone->uz_keg->uk_size,
1836 udata, flags) != 0) {
1837 uma_zfree_internal(zone, item, udata,
1838 SKIP_DTOR, ZFREE_STATFAIL |
1844 bzero(item, zone->uz_keg->uk_size);
1846 } else if (cache->uc_freebucket) {
1848 * We have run out of items in our allocbucket.
1849 * See if we can switch with our free bucket.
1851 if (cache->uc_freebucket->ub_cnt > 0) {
1852 #ifdef UMA_DEBUG_ALLOC
1853 printf("uma_zalloc: Swapping empty with"
1856 bucket = cache->uc_freebucket;
1857 cache->uc_freebucket = cache->uc_allocbucket;
1858 cache->uc_allocbucket = bucket;
1865 * Attempt to retrieve the item from the per-CPU cache has failed, so
1866 * we must go back to the zone. This requires the zone lock, so we
1867 * must drop the critical section, then re-acquire it when we go back
1868 * to the cache. Since the critical section is released, we may be
1869 * preempted or migrate. As such, make sure not to maintain any
1870 * thread-local state specific to the cache from prior to releasing
1871 * the critical section.
1877 cache = &zone->uz_cpu[cpu];
1878 bucket = cache->uc_allocbucket;
1879 if (bucket != NULL) {
1880 if (bucket->ub_cnt > 0) {
1884 bucket = cache->uc_freebucket;
1885 if (bucket != NULL && bucket->ub_cnt > 0) {
1891 /* Since we have locked the zone we may as well send back our stats */
1892 zone->uz_allocs += cache->uc_allocs;
1893 cache->uc_allocs = 0;
1894 zone->uz_frees += cache->uc_frees;
1895 cache->uc_frees = 0;
1897 /* Our old one is now a free bucket */
1898 if (cache->uc_allocbucket) {
1899 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
1900 ("uma_zalloc_arg: Freeing a non free bucket."));
1901 LIST_INSERT_HEAD(&zone->uz_free_bucket,
1902 cache->uc_allocbucket, ub_link);
1903 cache->uc_allocbucket = NULL;
1906 /* Check the free list for a new alloc bucket */
1907 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
1908 KASSERT(bucket->ub_cnt != 0,
1909 ("uma_zalloc_arg: Returning an empty bucket."));
1911 LIST_REMOVE(bucket, ub_link);
1912 cache->uc_allocbucket = bucket;
1916 /* We are no longer associated with this CPU. */
1919 /* Bump up our uz_count so we get here less */
1920 if (zone->uz_count < BUCKET_MAX)
1924 * Now lets just fill a bucket and put it on the free list. If that
1925 * works we'll restart the allocation from the begining.
1927 if (uma_zalloc_bucket(zone, flags)) {
1929 goto zalloc_restart;
1933 * We may not be able to get a bucket so return an actual item.
1936 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
1939 return (uma_zalloc_internal(zone, udata, flags));
1943 uma_zone_slab(uma_zone_t zone, int flags)
1951 * This is to prevent us from recursively trying to allocate
1952 * buckets. The problem is that if an allocation forces us to
1953 * grab a new bucket we will call page_alloc, which will go off
1954 * and cause the vm to allocate vm_map_entries. If we need new
1955 * buckets there too we will recurse in kmem_alloc and bad
1956 * things happen. So instead we return a NULL bucket, and make
1957 * the code that allocates buckets smart enough to deal with it
1959 * XXX: While we want this protection for the bucket zones so that
1960 * recursion from the VM is handled (and the calling code that
1961 * allocates buckets knows how to deal with it), we do not want
1962 * to prevent allocation from the slab header zones (slabzone
1963 * and slabrefzone) if uk_recurse is not zero for them. The
1964 * reason is that it could lead to NULL being returned for
1965 * slab header allocations even in the M_WAITOK case, and the
1966 * caller can't handle that.
1968 if (keg->uk_flags & UMA_ZFLAG_INTERNAL && keg->uk_recurse != 0)
1969 if (zone != slabzone && zone != slabrefzone && zone != zones)
1976 * Find a slab with some space. Prefer slabs that are partially
1977 * used over those that are totally full. This helps to reduce
1980 if (keg->uk_free != 0) {
1981 if (!LIST_EMPTY(&keg->uk_part_slab)) {
1982 slab = LIST_FIRST(&keg->uk_part_slab);
1984 slab = LIST_FIRST(&keg->uk_free_slab);
1985 LIST_REMOVE(slab, us_link);
1986 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
1993 * M_NOVM means don't ask at all!
1998 if (keg->uk_maxpages &&
1999 keg->uk_pages >= keg->uk_maxpages) {
2000 keg->uk_flags |= UMA_ZFLAG_FULL;
2002 if (flags & M_NOWAIT)
2005 msleep(keg, &keg->uk_lock, PVM,
2010 slab = slab_zalloc(zone, flags);
2014 * If we got a slab here it's safe to mark it partially used
2015 * and return. We assume that the caller is going to remove
2016 * at least one item.
2019 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2023 * We might not have been able to get a slab but another cpu
2024 * could have while we were unlocked. Check again before we
2027 if (flags & M_NOWAIT)
2034 uma_slab_alloc(uma_zone_t zone, uma_slab_t slab)
2037 uma_slabrefcnt_t slabref;
2043 freei = slab->us_firstfree;
2044 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2045 slabref = (uma_slabrefcnt_t)slab;
2046 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2048 slab->us_firstfree = slab->us_freelist[freei].us_item;
2050 item = slab->us_data + (keg->uk_rsize * freei);
2052 slab->us_freecount--;
2055 uma_dbg_alloc(zone, slab, item);
2057 /* Move this slab to the full list */
2058 if (slab->us_freecount == 0) {
2059 LIST_REMOVE(slab, us_link);
2060 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2067 uma_zalloc_bucket(uma_zone_t zone, int flags)
2069 uma_bucket_t bucket;
2072 int max, origflags = flags;
2075 * Try this zone's free list first so we don't allocate extra buckets.
2077 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2078 KASSERT(bucket->ub_cnt == 0,
2079 ("uma_zalloc_bucket: Bucket on free list is not empty."));
2080 LIST_REMOVE(bucket, ub_link);
2084 bflags = (flags & ~M_ZERO);
2085 if (zone->uz_keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2089 bucket = bucket_alloc(zone->uz_count, bflags);
2098 * This code is here to limit the number of simultaneous bucket fills
2099 * for any given zone to the number of per cpu caches in this zone. This
2100 * is done so that we don't allocate more memory than we really need.
2102 if (zone->uz_fills >= mp_ncpus)
2108 max = MIN(bucket->ub_entries, zone->uz_count);
2109 /* Try to keep the buckets totally full */
2110 saved = bucket->ub_cnt;
2111 while (bucket->ub_cnt < max &&
2112 (slab = uma_zone_slab(zone, flags)) != NULL) {
2113 while (slab->us_freecount && bucket->ub_cnt < max) {
2114 bucket->ub_bucket[bucket->ub_cnt++] =
2115 uma_slab_alloc(zone, slab);
2118 /* Don't block on the next fill */
2123 * We unlock here because we need to call the zone's init.
2124 * It should be safe to unlock because the slab dealt with
2125 * above is already on the appropriate list within the keg
2126 * and the bucket we filled is not yet on any list, so we
2129 if (zone->uz_init != NULL) {
2133 for (i = saved; i < bucket->ub_cnt; i++)
2134 if (zone->uz_init(bucket->ub_bucket[i],
2135 zone->uz_keg->uk_size, origflags) != 0)
2138 * If we couldn't initialize the whole bucket, put the
2139 * rest back onto the freelist.
2141 if (i != bucket->ub_cnt) {
2144 for (j = i; j < bucket->ub_cnt; j++) {
2145 uma_zfree_internal(zone, bucket->ub_bucket[j],
2146 NULL, SKIP_FINI, 0);
2148 bucket->ub_bucket[j] = NULL;
2157 if (bucket->ub_cnt != 0) {
2158 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2165 bucket_free(bucket);
2170 * Allocates an item for an internal zone
2173 * zone The zone to alloc for.
2174 * udata The data to be passed to the constructor.
2175 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2178 * NULL if there is no memory and M_NOWAIT is set
2179 * An item if successful
2183 uma_zalloc_internal(uma_zone_t zone, void *udata, int flags)
2192 #ifdef UMA_DEBUG_ALLOC
2193 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2197 slab = uma_zone_slab(zone, flags);
2204 item = uma_slab_alloc(zone, slab);
2211 * We have to call both the zone's init (not the keg's init)
2212 * and the zone's ctor. This is because the item is going from
2213 * a keg slab directly to the user, and the user is expecting it
2214 * to be both zone-init'd as well as zone-ctor'd.
2216 if (zone->uz_init != NULL) {
2217 if (zone->uz_init(item, keg->uk_size, flags) != 0) {
2218 uma_zfree_internal(zone, item, udata, SKIP_FINI,
2219 ZFREE_STATFAIL | ZFREE_STATFREE);
2223 if (zone->uz_ctor != NULL) {
2224 if (zone->uz_ctor(item, keg->uk_size, udata, flags) != 0) {
2225 uma_zfree_internal(zone, item, udata, SKIP_DTOR,
2226 ZFREE_STATFAIL | ZFREE_STATFREE);
2231 bzero(item, keg->uk_size);
2238 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2242 uma_bucket_t bucket;
2248 #ifdef UMA_DEBUG_ALLOC_1
2249 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2251 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2255 zone->uz_dtor(item, keg->uk_size, udata);
2258 if (keg->uk_flags & UMA_ZONE_MALLOC)
2259 uma_dbg_free(zone, udata, item);
2261 uma_dbg_free(zone, NULL, item);
2265 * The race here is acceptable. If we miss it we'll just have to wait
2266 * a little longer for the limits to be reset.
2268 if (keg->uk_flags & UMA_ZFLAG_FULL)
2269 goto zfree_internal;
2272 * If possible, free to the per-CPU cache. There are two
2273 * requirements for safe access to the per-CPU cache: (1) the thread
2274 * accessing the cache must not be preempted or yield during access,
2275 * and (2) the thread must not migrate CPUs without switching which
2276 * cache it accesses. We rely on a critical section to prevent
2277 * preemption and migration. We release the critical section in
2278 * order to acquire the zone mutex if we are unable to free to the
2279 * current cache; when we re-acquire the critical section, we must
2280 * detect and handle migration if it has occurred.
2285 cache = &zone->uz_cpu[cpu];
2288 bucket = cache->uc_freebucket;
2292 * Do we have room in our bucket? It is OK for this uz count
2293 * check to be slightly out of sync.
2296 if (bucket->ub_cnt < bucket->ub_entries) {
2297 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2298 ("uma_zfree: Freeing to non free bucket index."));
2299 bucket->ub_bucket[bucket->ub_cnt] = item;
2304 } else if (cache->uc_allocbucket) {
2305 #ifdef UMA_DEBUG_ALLOC
2306 printf("uma_zfree: Swapping buckets.\n");
2309 * We have run out of space in our freebucket.
2310 * See if we can switch with our alloc bucket.
2312 if (cache->uc_allocbucket->ub_cnt <
2313 cache->uc_freebucket->ub_cnt) {
2314 bucket = cache->uc_freebucket;
2315 cache->uc_freebucket = cache->uc_allocbucket;
2316 cache->uc_allocbucket = bucket;
2322 * We can get here for two reasons:
2324 * 1) The buckets are NULL
2325 * 2) The alloc and free buckets are both somewhat full.
2327 * We must go back the zone, which requires acquiring the zone lock,
2328 * which in turn means we must release and re-acquire the critical
2329 * section. Since the critical section is released, we may be
2330 * preempted or migrate. As such, make sure not to maintain any
2331 * thread-local state specific to the cache from prior to releasing
2332 * the critical section.
2338 cache = &zone->uz_cpu[cpu];
2339 if (cache->uc_freebucket != NULL) {
2340 if (cache->uc_freebucket->ub_cnt <
2341 cache->uc_freebucket->ub_entries) {
2345 if (cache->uc_allocbucket != NULL &&
2346 (cache->uc_allocbucket->ub_cnt <
2347 cache->uc_freebucket->ub_cnt)) {
2353 /* Since we have locked the zone we may as well send back our stats */
2354 zone->uz_allocs += cache->uc_allocs;
2355 cache->uc_allocs = 0;
2356 zone->uz_frees += cache->uc_frees;
2357 cache->uc_frees = 0;
2359 bucket = cache->uc_freebucket;
2360 cache->uc_freebucket = NULL;
2362 /* Can we throw this on the zone full list? */
2363 if (bucket != NULL) {
2364 #ifdef UMA_DEBUG_ALLOC
2365 printf("uma_zfree: Putting old bucket on the free list.\n");
2367 /* ub_cnt is pointing to the last free item */
2368 KASSERT(bucket->ub_cnt != 0,
2369 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2370 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2373 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2374 LIST_REMOVE(bucket, ub_link);
2376 cache->uc_freebucket = bucket;
2379 /* We are no longer associated with this CPU. */
2382 /* And the zone.. */
2385 #ifdef UMA_DEBUG_ALLOC
2386 printf("uma_zfree: Allocating new free bucket.\n");
2390 if (keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2392 bucket = bucket_alloc(zone->uz_count, bflags);
2395 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2402 * If nothing else caught this, we'll just do an internal free.
2405 uma_zfree_internal(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2411 * Frees an item to an INTERNAL zone or allocates a free bucket
2414 * zone The zone to free to
2415 * item The item we're freeing
2416 * udata User supplied data for the dtor
2417 * skip Skip dtors and finis
2420 uma_zfree_internal(uma_zone_t zone, void *item, void *udata,
2421 enum zfreeskip skip, int flags)
2424 uma_slabrefcnt_t slabref;
2431 if (skip < SKIP_DTOR && zone->uz_dtor)
2432 zone->uz_dtor(item, keg->uk_size, udata);
2433 if (skip < SKIP_FINI && zone->uz_fini)
2434 zone->uz_fini(item, keg->uk_size);
2438 if (flags & ZFREE_STATFAIL)
2440 if (flags & ZFREE_STATFREE)
2443 if (!(keg->uk_flags & UMA_ZONE_MALLOC)) {
2444 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2445 if (keg->uk_flags & UMA_ZONE_HASH)
2446 slab = hash_sfind(&keg->uk_hash, mem);
2448 mem += keg->uk_pgoff;
2449 slab = (uma_slab_t)mem;
2452 slab = (uma_slab_t)udata;
2455 /* Do we need to remove from any lists? */
2456 if (slab->us_freecount+1 == keg->uk_ipers) {
2457 LIST_REMOVE(slab, us_link);
2458 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2459 } else if (slab->us_freecount == 0) {
2460 LIST_REMOVE(slab, us_link);
2461 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2464 /* Slab management stuff */
2465 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2470 uma_dbg_free(zone, slab, item);
2473 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2474 slabref = (uma_slabrefcnt_t)slab;
2475 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2477 slab->us_freelist[freei].us_item = slab->us_firstfree;
2479 slab->us_firstfree = freei;
2480 slab->us_freecount++;
2482 /* Zone statistics */
2485 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2486 if (keg->uk_pages < keg->uk_maxpages)
2487 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2490 * We can handle one more allocation. Since we're clearing ZFLAG_FULL,
2491 * wake up all procs blocked on pages. This should be uncommon, so
2492 * keeping this simple for now (rather than adding count of blocked
2503 uma_zone_set_max(uma_zone_t zone, int nitems)
2509 if (keg->uk_ppera > 1)
2510 keg->uk_maxpages = nitems * keg->uk_ppera;
2512 keg->uk_maxpages = nitems / keg->uk_ipers;
2514 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2522 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2525 KASSERT(zone->uz_keg->uk_pages == 0,
2526 ("uma_zone_set_init on non-empty keg"));
2527 zone->uz_keg->uk_init = uminit;
2533 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2536 KASSERT(zone->uz_keg->uk_pages == 0,
2537 ("uma_zone_set_fini on non-empty keg"));
2538 zone->uz_keg->uk_fini = fini;
2544 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2547 KASSERT(zone->uz_keg->uk_pages == 0,
2548 ("uma_zone_set_zinit on non-empty keg"));
2549 zone->uz_init = zinit;
2555 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2558 KASSERT(zone->uz_keg->uk_pages == 0,
2559 ("uma_zone_set_zfini on non-empty keg"));
2560 zone->uz_fini = zfini;
2565 /* XXX uk_freef is not actually used with the zone locked */
2567 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2570 zone->uz_keg->uk_freef = freef;
2575 /* XXX uk_allocf is not actually used with the zone locked */
2577 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2580 zone->uz_keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2581 zone->uz_keg->uk_allocf = allocf;
2587 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2594 pages = count / keg->uk_ipers;
2596 if (pages * keg->uk_ipers < count)
2599 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2604 obj = vm_object_allocate(OBJT_DEFAULT,
2607 VM_OBJECT_LOCK_INIT(obj, "uma object");
2608 _vm_object_allocate(OBJT_DEFAULT,
2614 keg->uk_maxpages = pages;
2615 keg->uk_allocf = obj_alloc;
2616 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2623 uma_prealloc(uma_zone_t zone, int items)
2631 slabs = items / keg->uk_ipers;
2632 if (slabs * keg->uk_ipers < items)
2635 slab = slab_zalloc(zone, M_WAITOK);
2636 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2644 uma_find_refcnt(uma_zone_t zone, void *item)
2646 uma_slabrefcnt_t slabref;
2652 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
2654 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
2655 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
2656 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
2658 refcnt = &slabref->us_freelist[idx].us_refcnt;
2667 printf("UMA: vm asked us to release pages!\n");
2670 zone_foreach(zone_drain);
2672 * Some slabs may have been freed but this zone will be visited early
2673 * we visit again so that we can free pages that are empty once other
2674 * zones are drained. We have to do the same for buckets.
2676 zone_drain(slabzone);
2677 zone_drain(slabrefzone);
2678 bucket_zone_drain();
2683 uma_zone_exhausted(uma_zone_t zone)
2688 full = (zone->uz_keg->uk_flags & UMA_ZFLAG_FULL);
2694 uma_zone_exhausted_nolock(uma_zone_t zone)
2696 return (zone->uz_keg->uk_flags & UMA_ZFLAG_FULL);
2700 uma_large_malloc(int size, int wait)
2706 slab = uma_zalloc_internal(slabzone, NULL, wait);
2709 mem = page_alloc(NULL, size, &flags, wait);
2711 vsetslab((vm_offset_t)mem, slab);
2712 slab->us_data = mem;
2713 slab->us_flags = flags | UMA_SLAB_MALLOC;
2714 slab->us_size = size;
2716 uma_zfree_internal(slabzone, slab, NULL, SKIP_NONE,
2717 ZFREE_STATFAIL | ZFREE_STATFREE);
2724 uma_large_free(uma_slab_t slab)
2726 vsetobj((vm_offset_t)slab->us_data, kmem_object);
2727 page_free(slab->us_data, slab->us_size, slab->us_flags);
2728 uma_zfree_internal(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
2732 uma_print_stats(void)
2734 zone_foreach(uma_print_zone);
2738 slab_print(uma_slab_t slab)
2740 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
2741 slab->us_keg, slab->us_data, slab->us_freecount,
2742 slab->us_firstfree);
2746 cache_print(uma_cache_t cache)
2748 printf("alloc: %p(%d), free: %p(%d)\n",
2749 cache->uc_allocbucket,
2750 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
2751 cache->uc_freebucket,
2752 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
2756 uma_print_zone(uma_zone_t zone)
2764 printf("%s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
2765 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
2766 keg->uk_ipers, keg->uk_ppera,
2767 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
2768 printf("Part slabs:\n");
2769 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
2771 printf("Free slabs:\n");
2772 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
2774 printf("Full slabs:\n");
2775 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
2777 for (i = 0; i <= mp_maxid; i++) {
2780 cache = &zone->uz_cpu[i];
2781 printf("CPU %d Cache:\n", i);
2788 * Generate statistics across both the zone and its per-cpu cache's. Return
2789 * desired statistics if the pointer is non-NULL for that statistic.
2791 * Note: does not update the zone statistics, as it can't safely clear the
2792 * per-CPU cache statistic.
2794 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
2795 * safe from off-CPU; we should modify the caches to track this information
2796 * directly so that we don't have to.
2799 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
2803 u_int64_t allocs, frees;
2808 for (cpu = 0; cpu <= mp_maxid; cpu++) {
2809 if (CPU_ABSENT(cpu))
2811 cache = &z->uz_cpu[cpu];
2812 if (cache->uc_allocbucket != NULL)
2813 cachefree += cache->uc_allocbucket->ub_cnt;
2814 if (cache->uc_freebucket != NULL)
2815 cachefree += cache->uc_freebucket->ub_cnt;
2816 allocs += cache->uc_allocs;
2817 frees += cache->uc_frees;
2819 allocs += z->uz_allocs;
2820 frees += z->uz_frees;
2821 if (cachefreep != NULL)
2822 *cachefreep = cachefree;
2823 if (allocsp != NULL)
2831 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
2839 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2840 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2843 mtx_unlock(&uma_mtx);
2844 return (sysctl_handle_int(oidp, &count, 0, req));
2848 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
2850 struct uma_stream_header ush;
2851 struct uma_type_header uth;
2852 struct uma_percpu_stat ups;
2853 uma_bucket_t bucket;
2859 int buflen, count, error, i;
2863 mtx_assert(&uma_mtx, MA_OWNED);
2865 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2866 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2869 mtx_unlock(&uma_mtx);
2871 buflen = sizeof(ush) + count * (sizeof(uth) + sizeof(ups) *
2872 (mp_maxid + 1)) + 1;
2873 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
2877 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2878 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2882 free(buffer, M_TEMP);
2887 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
2890 * Insert stream header.
2892 bzero(&ush, sizeof(ush));
2893 ush.ush_version = UMA_STREAM_VERSION;
2894 ush.ush_maxcpus = (mp_maxid + 1);
2895 ush.ush_count = count;
2896 if (sbuf_bcat(&sbuf, &ush, sizeof(ush)) < 0) {
2897 mtx_unlock(&uma_mtx);
2902 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2903 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
2904 bzero(&uth, sizeof(uth));
2906 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
2907 uth.uth_align = kz->uk_align;
2908 uth.uth_pages = kz->uk_pages;
2909 uth.uth_keg_free = kz->uk_free;
2910 uth.uth_size = kz->uk_size;
2911 uth.uth_rsize = kz->uk_rsize;
2912 uth.uth_maxpages = kz->uk_maxpages;
2913 if (kz->uk_ppera > 1)
2914 uth.uth_limit = kz->uk_maxpages /
2917 uth.uth_limit = kz->uk_maxpages *
2921 * A zone is secondary is it is not the first entry
2922 * on the keg's zone list.
2924 if ((kz->uk_flags & UMA_ZONE_SECONDARY) &&
2925 (LIST_FIRST(&kz->uk_zones) != z))
2926 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
2928 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
2929 uth.uth_zone_free += bucket->ub_cnt;
2930 uth.uth_allocs = z->uz_allocs;
2931 uth.uth_frees = z->uz_frees;
2932 uth.uth_fails = z->uz_fails;
2933 if (sbuf_bcat(&sbuf, &uth, sizeof(uth)) < 0) {
2935 mtx_unlock(&uma_mtx);
2940 * While it is not normally safe to access the cache
2941 * bucket pointers while not on the CPU that owns the
2942 * cache, we only allow the pointers to be exchanged
2943 * without the zone lock held, not invalidated, so
2944 * accept the possible race associated with bucket
2945 * exchange during monitoring.
2947 for (i = 0; i < (mp_maxid + 1); i++) {
2948 bzero(&ups, sizeof(ups));
2949 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
2953 cache = &z->uz_cpu[i];
2954 if (cache->uc_allocbucket != NULL)
2955 ups.ups_cache_free +=
2956 cache->uc_allocbucket->ub_cnt;
2957 if (cache->uc_freebucket != NULL)
2958 ups.ups_cache_free +=
2959 cache->uc_freebucket->ub_cnt;
2960 ups.ups_allocs = cache->uc_allocs;
2961 ups.ups_frees = cache->uc_frees;
2963 if (sbuf_bcat(&sbuf, &ups, sizeof(ups)) < 0) {
2965 mtx_unlock(&uma_mtx);
2973 mtx_unlock(&uma_mtx);
2975 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
2977 free(buffer, M_TEMP);
2982 DB_SHOW_COMMAND(uma, db_show_uma)
2984 u_int64_t allocs, frees;
2985 uma_bucket_t bucket;
2990 db_printf("%18s %8s %8s %8s %12s\n", "Zone", "Size", "Used", "Free",
2992 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2993 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
2994 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
2995 allocs = z->uz_allocs;
2996 frees = z->uz_frees;
2999 uma_zone_sumstat(z, &cachefree, &allocs,
3001 if (!((kz->uk_flags & UMA_ZONE_SECONDARY) &&
3002 (LIST_FIRST(&kz->uk_zones) != z)))
3003 cachefree += kz->uk_free;
3004 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3005 cachefree += bucket->ub_cnt;
3006 db_printf("%18s %8ju %8jd %8d %12ju\n", z->uz_name,
3007 (uintmax_t)kz->uk_size,
3008 (intmax_t)(allocs - frees), cachefree,