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-2005 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 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
117 * Are we allowed to allocate buckets?
119 static int bucketdisable = 1;
121 /* Linked list of all kegs in the system */
122 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(&uma_kegs);
124 /* This mutex protects the keg list */
125 static struct mtx uma_mtx;
127 /* Linked list of boot time pages */
128 static LIST_HEAD(,uma_slab) uma_boot_pages =
129 LIST_HEAD_INITIALIZER(&uma_boot_pages);
131 /* This mutex protects the boot time pages list */
132 static struct mtx uma_boot_pages_mtx;
134 /* Is the VM done starting up? */
135 static int booted = 0;
137 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
138 static u_int uma_max_ipers;
139 static u_int uma_max_ipers_ref;
142 * This is the handle used to schedule events that need to happen
143 * outside of the allocation fast path.
145 static struct callout uma_callout;
146 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
149 * This structure is passed as the zone ctor arg so that I don't have to create
150 * a special allocation function just for zones.
152 struct uma_zctor_args {
164 struct uma_kctor_args {
173 struct uma_bucket_zone {
179 #define BUCKET_MAX 128
181 struct uma_bucket_zone bucket_zones[] = {
182 { NULL, "16 Bucket", 16 },
183 { NULL, "32 Bucket", 32 },
184 { NULL, "64 Bucket", 64 },
185 { NULL, "128 Bucket", 128 },
189 #define BUCKET_SHIFT 4
190 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
193 * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
194 * of approximately the right size.
196 static uint8_t bucket_size[BUCKET_ZONES];
199 * Flags and enumerations to be passed to internal functions.
201 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
203 #define ZFREE_STATFAIL 0x00000001 /* Update zone failure statistic. */
204 #define ZFREE_STATFREE 0x00000002 /* Update zone free statistic. */
208 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
209 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
210 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
211 static void page_free(void *, int, u_int8_t);
212 static uma_slab_t slab_zalloc(uma_zone_t, int);
213 static void cache_drain(uma_zone_t);
214 static void bucket_drain(uma_zone_t, uma_bucket_t);
215 static void bucket_cache_drain(uma_zone_t zone);
216 static int keg_ctor(void *, int, void *, int);
217 static void keg_dtor(void *, int, void *);
218 static int zone_ctor(void *, int, void *, int);
219 static void zone_dtor(void *, int, void *);
220 static int zero_init(void *, int, int);
221 static void zone_small_init(uma_zone_t zone);
222 static void zone_large_init(uma_zone_t zone);
223 static void zone_foreach(void (*zfunc)(uma_zone_t));
224 static void zone_timeout(uma_zone_t zone);
225 static int hash_alloc(struct uma_hash *);
226 static int hash_expand(struct uma_hash *, struct uma_hash *);
227 static void hash_free(struct uma_hash *hash);
228 static void uma_timeout(void *);
229 static void uma_startup3(void);
230 static void *uma_zalloc_internal(uma_zone_t, void *, int);
231 static void uma_zfree_internal(uma_zone_t, void *, void *, enum zfreeskip,
233 static void bucket_enable(void);
234 static void bucket_init(void);
235 static uma_bucket_t bucket_alloc(int, int);
236 static void bucket_free(uma_bucket_t);
237 static void bucket_zone_drain(void);
238 static int uma_zalloc_bucket(uma_zone_t zone, int flags);
239 static uma_slab_t uma_zone_slab(uma_zone_t zone, int flags);
240 static void *uma_slab_alloc(uma_zone_t zone, uma_slab_t slab);
241 static uma_zone_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
242 uma_fini fini, int align, u_int32_t flags);
244 void uma_print_zone(uma_zone_t);
245 void uma_print_stats(void);
246 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
247 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
248 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
251 static int nosleepwithlocks = 1;
252 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
253 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
255 static int nosleepwithlocks = 0;
256 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
257 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
259 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD,
260 NULL, 0, sysctl_vm_zone, "A", "Zone Info");
261 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
263 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
264 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
266 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
267 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
270 * This routine checks to see whether or not it's safe to enable buckets.
276 if (cnt.v_free_count < cnt.v_free_min)
283 * Initialize bucket_zones, the array of zones of buckets of various sizes.
285 * For each zone, calculate the memory required for each bucket, consisting
286 * of the header and an array of pointers. Initialize bucket_size[] to point
287 * the range of appropriate bucket sizes at the zone.
292 struct uma_bucket_zone *ubz;
296 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
299 ubz = &bucket_zones[j];
300 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
301 size += sizeof(void *) * ubz->ubz_entries;
302 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
303 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
304 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
305 bucket_size[i >> BUCKET_SHIFT] = j;
310 * Given a desired number of entries for a bucket, return the zone from which
311 * to allocate the bucket.
313 static struct uma_bucket_zone *
314 bucket_zone_lookup(int entries)
318 idx = howmany(entries, 1 << BUCKET_SHIFT);
319 return (&bucket_zones[bucket_size[idx]]);
323 bucket_alloc(int entries, int bflags)
325 struct uma_bucket_zone *ubz;
329 * This is to stop us from allocating per cpu buckets while we're
330 * running out of vm.boot_pages. Otherwise, we would exhaust the
331 * boot pages. This also prevents us from allocating buckets in
332 * low memory situations.
337 ubz = bucket_zone_lookup(entries);
338 bucket = uma_zalloc_internal(ubz->ubz_zone, NULL, bflags);
341 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
344 bucket->ub_entries = ubz->ubz_entries;
351 bucket_free(uma_bucket_t bucket)
353 struct uma_bucket_zone *ubz;
355 ubz = bucket_zone_lookup(bucket->ub_entries);
356 uma_zfree_internal(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
361 bucket_zone_drain(void)
363 struct uma_bucket_zone *ubz;
365 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
366 zone_drain(ubz->ubz_zone);
371 * Routine called by timeout which is used to fire off some time interval
372 * based calculations. (stats, hash size, etc.)
381 uma_timeout(void *unused)
384 zone_foreach(zone_timeout);
386 /* Reschedule this event */
387 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
391 * Routine to perform timeout driven calculations. This expands the
392 * hashes and does per cpu statistics aggregation.
395 * zone The zone to operate on
401 zone_timeout(uma_zone_t zone)
410 * Expand the zone hash table.
412 * This is done if the number of slabs is larger than the hash size.
413 * What I'm trying to do here is completely reduce collisions. This
414 * may be a little aggressive. Should I allow for two collisions max?
417 if (keg->uk_flags & UMA_ZONE_HASH &&
418 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
419 struct uma_hash newhash;
420 struct uma_hash oldhash;
424 * This is so involved because allocating and freeing
425 * while the zone lock is held will lead to deadlock.
426 * I have to do everything in stages and check for
429 newhash = keg->uk_hash;
431 ret = hash_alloc(&newhash);
434 if (hash_expand(&keg->uk_hash, &newhash)) {
435 oldhash = keg->uk_hash;
436 keg->uk_hash = newhash;
449 * Allocate and zero fill the next sized hash table from the appropriate
453 * hash A new hash structure with the old hash size in uh_hashsize
456 * 1 on sucess and 0 on failure.
459 hash_alloc(struct uma_hash *hash)
464 oldsize = hash->uh_hashsize;
466 /* We're just going to go to a power of two greater */
468 hash->uh_hashsize = oldsize * 2;
469 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
470 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
471 M_UMAHASH, M_NOWAIT);
473 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
474 hash->uh_slab_hash = uma_zalloc_internal(hashzone, NULL,
476 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
478 if (hash->uh_slab_hash) {
479 bzero(hash->uh_slab_hash, alloc);
480 hash->uh_hashmask = hash->uh_hashsize - 1;
488 * Expands the hash table for HASH zones. This is done from zone_timeout
489 * to reduce collisions. This must not be done in the regular allocation
490 * path, otherwise, we can recurse on the vm while allocating pages.
493 * oldhash The hash you want to expand
494 * newhash The hash structure for the new table
502 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
508 if (!newhash->uh_slab_hash)
511 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
515 * I need to investigate hash algorithms for resizing without a
519 for (i = 0; i < oldhash->uh_hashsize; i++)
520 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
521 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
522 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
523 hval = UMA_HASH(newhash, slab->us_data);
524 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
532 * Free the hash bucket to the appropriate backing store.
535 * slab_hash The hash bucket we're freeing
536 * hashsize The number of entries in that hash bucket
542 hash_free(struct uma_hash *hash)
544 if (hash->uh_slab_hash == NULL)
546 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
547 uma_zfree_internal(hashzone,
548 hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
550 free(hash->uh_slab_hash, M_UMAHASH);
554 * Frees all outstanding items in a bucket
557 * zone The zone to free to, must be unlocked.
558 * bucket The free/alloc bucket with items, cpu queue must be locked.
565 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
577 /* We have to lookup the slab again for malloc.. */
578 if (zone->uz_keg->uk_flags & UMA_ZONE_MALLOC)
581 while (bucket->ub_cnt > 0) {
583 item = bucket->ub_bucket[bucket->ub_cnt];
585 bucket->ub_bucket[bucket->ub_cnt] = NULL;
586 KASSERT(item != NULL,
587 ("bucket_drain: botched ptr, item is NULL"));
590 * This is extremely inefficient. The slab pointer was passed
591 * to uma_zfree_arg, but we lost it because the buckets don't
592 * hold them. This will go away when free() gets a size passed
596 slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
597 uma_zfree_internal(zone, item, slab, SKIP_DTOR, 0);
602 * Drains the per cpu caches for a zone.
604 * NOTE: This may only be called while the zone is being turn down, and not
605 * during normal operation. This is necessary in order that we do not have
606 * to migrate CPUs to drain the per-CPU caches.
609 * zone The zone to drain, must be unlocked.
615 cache_drain(uma_zone_t zone)
621 * XXX: It is safe to not lock the per-CPU caches, because we're
622 * tearing down the zone anyway. I.e., there will be no further use
623 * of the caches at this point.
625 * XXX: It would good to be able to assert that the zone is being
626 * torn down to prevent improper use of cache_drain().
628 * XXX: We lock the zone before passing into bucket_cache_drain() as
629 * it is used elsewhere. Should the tear-down path be made special
630 * there in some form?
632 for (cpu = 0; cpu <= mp_maxid; cpu++) {
635 cache = &zone->uz_cpu[cpu];
636 bucket_drain(zone, cache->uc_allocbucket);
637 bucket_drain(zone, cache->uc_freebucket);
638 if (cache->uc_allocbucket != NULL)
639 bucket_free(cache->uc_allocbucket);
640 if (cache->uc_freebucket != NULL)
641 bucket_free(cache->uc_freebucket);
642 cache->uc_allocbucket = cache->uc_freebucket = NULL;
645 bucket_cache_drain(zone);
650 * Drain the cached buckets from a zone. Expects a locked zone on entry.
653 bucket_cache_drain(uma_zone_t zone)
658 * Drain the bucket queues and free the buckets, we just keep two per
661 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
662 LIST_REMOVE(bucket, ub_link);
664 bucket_drain(zone, bucket);
669 /* Now we do the free queue.. */
670 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
671 LIST_REMOVE(bucket, ub_link);
677 * Frees pages from a zone back to the system. This is done on demand from
678 * the pageout daemon.
681 * zone The zone to free pages from
682 * all Should we drain all items?
688 zone_drain(uma_zone_t zone)
690 struct slabhead freeslabs = { 0 };
701 * We don't want to take pages from statically allocated zones at this
704 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
710 printf("%s free items: %u\n", zone->uz_name, keg->uk_free);
712 bucket_cache_drain(zone);
713 if (keg->uk_free == 0)
716 slab = LIST_FIRST(&keg->uk_free_slab);
718 n = LIST_NEXT(slab, us_link);
720 /* We have no where to free these to */
721 if (slab->us_flags & UMA_SLAB_BOOT) {
726 LIST_REMOVE(slab, us_link);
727 keg->uk_pages -= keg->uk_ppera;
728 keg->uk_free -= keg->uk_ipers;
730 if (keg->uk_flags & UMA_ZONE_HASH)
731 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
733 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
740 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
741 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
743 for (i = 0; i < keg->uk_ipers; i++)
745 slab->us_data + (keg->uk_rsize * i),
747 flags = slab->us_flags;
750 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
751 (keg->uk_flags & UMA_ZONE_REFCNT)) {
754 if (flags & UMA_SLAB_KMEM)
758 for (i = 0; i < keg->uk_ppera; i++)
759 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
762 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
763 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
764 SKIP_NONE, ZFREE_STATFREE);
766 printf("%s: Returning %d bytes.\n",
767 zone->uz_name, UMA_SLAB_SIZE * keg->uk_ppera);
769 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
774 * Allocate a new slab for a zone. This does not insert the slab onto a list.
777 * zone The zone to allocate slabs for
778 * wait Shall we wait?
781 * The slab that was allocated or NULL if there is no memory and the
782 * caller specified M_NOWAIT.
785 slab_zalloc(uma_zone_t zone, int wait)
787 uma_slabrefcnt_t slabref;
798 printf("slab_zalloc: Allocating a new slab for %s\n", zone->uz_name);
802 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
803 slab = uma_zalloc_internal(keg->uk_slabzone, NULL, wait);
811 * This reproduces the old vm_zone behavior of zero filling pages the
812 * first time they are added to a zone.
814 * Malloced items are zeroed in uma_zalloc.
817 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
822 mem = keg->uk_allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE,
825 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
826 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
827 SKIP_NONE, ZFREE_STATFREE);
832 /* Point the slab into the allocated memory */
833 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
834 slab = (uma_slab_t )(mem + keg->uk_pgoff);
836 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
837 (keg->uk_flags & UMA_ZONE_REFCNT))
838 for (i = 0; i < keg->uk_ppera; i++)
839 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
843 slab->us_freecount = keg->uk_ipers;
844 slab->us_firstfree = 0;
845 slab->us_flags = flags;
847 if (keg->uk_flags & UMA_ZONE_REFCNT) {
848 slabref = (uma_slabrefcnt_t)slab;
849 for (i = 0; i < keg->uk_ipers; i++) {
850 slabref->us_freelist[i].us_refcnt = 0;
851 slabref->us_freelist[i].us_item = i+1;
854 for (i = 0; i < keg->uk_ipers; i++)
855 slab->us_freelist[i].us_item = i+1;
858 if (keg->uk_init != NULL) {
859 for (i = 0; i < keg->uk_ipers; i++)
860 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
861 keg->uk_size, wait) != 0)
863 if (i != keg->uk_ipers) {
864 if (keg->uk_fini != NULL) {
865 for (i--; i > -1; i--)
866 keg->uk_fini(slab->us_data +
870 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
871 (keg->uk_flags & UMA_ZONE_REFCNT)) {
874 if (flags & UMA_SLAB_KMEM)
878 for (i = 0; i < keg->uk_ppera; i++)
879 vsetobj((vm_offset_t)mem +
880 (i * PAGE_SIZE), obj);
882 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
883 uma_zfree_internal(keg->uk_slabzone, slab,
884 NULL, SKIP_NONE, ZFREE_STATFREE);
885 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
893 if (keg->uk_flags & UMA_ZONE_HASH)
894 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
896 keg->uk_pages += keg->uk_ppera;
897 keg->uk_free += keg->uk_ipers;
903 * This function is intended to be used early on in place of page_alloc() so
904 * that we may use the boot time page cache to satisfy allocations before
908 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
916 * Check our small startup cache to see if it has pages remaining.
918 mtx_lock(&uma_boot_pages_mtx);
919 if ((tmps = LIST_FIRST(&uma_boot_pages)) != NULL) {
920 LIST_REMOVE(tmps, us_link);
921 mtx_unlock(&uma_boot_pages_mtx);
922 *pflag = tmps->us_flags;
923 return (tmps->us_data);
925 mtx_unlock(&uma_boot_pages_mtx);
927 panic("UMA: Increase vm.boot_pages");
929 * Now that we've booted reset these users to their real allocator.
931 #ifdef UMA_MD_SMALL_ALLOC
932 keg->uk_allocf = uma_small_alloc;
934 keg->uk_allocf = page_alloc;
936 return keg->uk_allocf(zone, bytes, pflag, wait);
940 * Allocates a number of pages from the system
944 * bytes The number of bytes requested
945 * wait Shall we wait?
948 * A pointer to the alloced memory or possibly
949 * NULL if M_NOWAIT is set.
952 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
954 void *p; /* Returned page */
956 *pflag = UMA_SLAB_KMEM;
957 p = (void *) kmem_malloc(kmem_map, bytes, wait);
963 * Allocates a number of pages from within an object
967 * bytes The number of bytes requested
968 * wait Shall we wait?
971 * A pointer to the alloced memory or possibly
972 * NULL if M_NOWAIT is set.
975 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
978 vm_offset_t retkva, zkva;
980 int pages, startpages;
982 object = zone->uz_keg->uk_obj;
986 * This looks a little weird since we're getting one page at a time.
988 VM_OBJECT_LOCK(object);
989 p = TAILQ_LAST(&object->memq, pglist);
990 pages = p != NULL ? p->pindex + 1 : 0;
992 zkva = zone->uz_keg->uk_kva + pages * PAGE_SIZE;
993 for (; bytes > 0; bytes -= PAGE_SIZE) {
994 p = vm_page_alloc(object, pages,
995 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
997 if (pages != startpages)
998 pmap_qremove(retkva, pages - startpages);
999 while (pages != startpages) {
1001 p = TAILQ_LAST(&object->memq, pglist);
1002 vm_page_lock_queues();
1003 vm_page_unwire(p, 0);
1005 vm_page_unlock_queues();
1010 pmap_qenter(zkva, &p, 1);
1017 VM_OBJECT_UNLOCK(object);
1018 *flags = UMA_SLAB_PRIV;
1020 return ((void *)retkva);
1024 * Frees a number of pages to the system
1027 * mem A pointer to the memory to be freed
1028 * size The size of the memory being freed
1029 * flags The original p->us_flags field
1035 page_free(void *mem, int size, u_int8_t flags)
1039 if (flags & UMA_SLAB_KMEM)
1042 panic("UMA: page_free used with invalid flags %d\n", flags);
1044 kmem_free(map, (vm_offset_t)mem, size);
1048 * Zero fill initializer
1050 * Arguments/Returns follow uma_init specifications
1053 zero_init(void *mem, int size, int flags)
1060 * Finish creating a small uma zone. This calculates ipers, and the zone size.
1063 * zone The zone we should initialize
1069 zone_small_init(uma_zone_t zone)
1078 KASSERT(keg != NULL, ("Keg is null in zone_small_init"));
1079 rsize = keg->uk_size;
1081 if (rsize < UMA_SMALLEST_UNIT)
1082 rsize = UMA_SMALLEST_UNIT;
1083 if (rsize & keg->uk_align)
1084 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1086 keg->uk_rsize = rsize;
1089 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1090 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1091 shsize = sizeof(struct uma_slab_refcnt);
1093 rsize += UMA_FRITM_SZ; /* Account for linkage */
1094 shsize = sizeof(struct uma_slab);
1097 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1098 KASSERT(keg->uk_ipers != 0, ("zone_small_init: ipers is 0"));
1099 memused = keg->uk_ipers * rsize + shsize;
1100 wastedspace = UMA_SLAB_SIZE - memused;
1103 * We can't do OFFPAGE if we're internal or if we've been
1104 * asked to not go to the VM for buckets. If we do this we
1105 * may end up going to the VM (kmem_map) for slabs which we
1106 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1107 * result of UMA_ZONE_VM, which clearly forbids it.
1109 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1110 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1113 if ((wastedspace >= UMA_MAX_WASTE) &&
1114 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1115 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1116 KASSERT(keg->uk_ipers <= 255,
1117 ("zone_small_init: keg->uk_ipers too high!"));
1119 printf("UMA decided we need offpage slab headers for "
1120 "zone: %s, calculated wastedspace = %d, "
1121 "maximum wasted space allowed = %d, "
1122 "calculated ipers = %d, "
1123 "new wasted space = %d\n", zone->uz_name, wastedspace,
1124 UMA_MAX_WASTE, keg->uk_ipers,
1125 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1127 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1128 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1129 keg->uk_flags |= UMA_ZONE_HASH;
1134 * Finish creating a large (> UMA_SLAB_SIZE) uma zone. Just give in and do
1135 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1139 * zone The zone we should initialize
1145 zone_large_init(uma_zone_t zone)
1152 KASSERT(keg != NULL, ("Keg is null in zone_large_init"));
1153 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1154 ("zone_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY zone"));
1156 pages = keg->uk_size / UMA_SLAB_SIZE;
1158 /* Account for remainder */
1159 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1162 keg->uk_ppera = pages;
1165 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1166 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1167 keg->uk_flags |= UMA_ZONE_HASH;
1169 keg->uk_rsize = keg->uk_size;
1173 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1174 * the keg onto the global keg list.
1176 * Arguments/Returns follow uma_ctor specifications
1177 * udata Actually uma_kctor_args
1180 keg_ctor(void *mem, int size, void *udata, int flags)
1182 struct uma_kctor_args *arg = udata;
1183 uma_keg_t keg = mem;
1187 keg->uk_size = arg->size;
1188 keg->uk_init = arg->uminit;
1189 keg->uk_fini = arg->fini;
1190 keg->uk_align = arg->align;
1193 keg->uk_flags = arg->flags;
1194 keg->uk_allocf = page_alloc;
1195 keg->uk_freef = page_free;
1196 keg->uk_recurse = 0;
1197 keg->uk_slabzone = NULL;
1200 * The master zone is passed to us at keg-creation time.
1205 if (arg->flags & UMA_ZONE_VM)
1206 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1208 if (arg->flags & UMA_ZONE_ZINIT)
1209 keg->uk_init = zero_init;
1212 * The +UMA_FRITM_SZ added to uk_size is to account for the
1213 * linkage that is added to the size in zone_small_init(). If
1214 * we don't account for this here then we may end up in
1215 * zone_small_init() with a calculated 'ipers' of 0.
1217 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1218 if ((keg->uk_size+UMA_FRITMREF_SZ) >
1219 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1220 zone_large_init(zone);
1222 zone_small_init(zone);
1224 if ((keg->uk_size+UMA_FRITM_SZ) >
1225 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1226 zone_large_init(zone);
1228 zone_small_init(zone);
1231 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1232 if (keg->uk_flags & UMA_ZONE_REFCNT)
1233 keg->uk_slabzone = slabrefzone;
1235 keg->uk_slabzone = slabzone;
1239 * If we haven't booted yet we need allocations to go through the
1240 * startup cache until the vm is ready.
1242 if (keg->uk_ppera == 1) {
1243 #ifdef UMA_MD_SMALL_ALLOC
1244 keg->uk_allocf = uma_small_alloc;
1245 keg->uk_freef = uma_small_free;
1248 keg->uk_allocf = startup_alloc;
1252 * Initialize keg's lock (shared among zones) through
1255 zone->uz_lock = &keg->uk_lock;
1256 if (arg->flags & UMA_ZONE_MTXCLASS)
1257 ZONE_LOCK_INIT(zone, 1);
1259 ZONE_LOCK_INIT(zone, 0);
1262 * If we're putting the slab header in the actual page we need to
1263 * figure out where in each page it goes. This calculates a right
1264 * justified offset into the memory on an ALIGN_PTR boundary.
1266 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1269 /* Size of the slab struct and free list */
1270 if (keg->uk_flags & UMA_ZONE_REFCNT)
1271 totsize = sizeof(struct uma_slab_refcnt) +
1272 keg->uk_ipers * UMA_FRITMREF_SZ;
1274 totsize = sizeof(struct uma_slab) +
1275 keg->uk_ipers * UMA_FRITM_SZ;
1277 if (totsize & UMA_ALIGN_PTR)
1278 totsize = (totsize & ~UMA_ALIGN_PTR) +
1279 (UMA_ALIGN_PTR + 1);
1280 keg->uk_pgoff = UMA_SLAB_SIZE - totsize;
1282 if (keg->uk_flags & UMA_ZONE_REFCNT)
1283 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1284 + keg->uk_ipers * UMA_FRITMREF_SZ;
1286 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1287 + keg->uk_ipers * UMA_FRITM_SZ;
1290 * The only way the following is possible is if with our
1291 * UMA_ALIGN_PTR adjustments we are now bigger than
1292 * UMA_SLAB_SIZE. I haven't checked whether this is
1293 * mathematically possible for all cases, so we make
1296 if (totsize > UMA_SLAB_SIZE) {
1297 printf("zone %s ipers %d rsize %d size %d\n",
1298 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1300 panic("UMA slab won't fit.\n");
1304 if (keg->uk_flags & UMA_ZONE_HASH)
1305 hash_alloc(&keg->uk_hash);
1308 printf("%s(%p) size = %d ipers = %d ppera = %d pgoff = %d\n",
1309 zone->uz_name, zone,
1310 keg->uk_size, keg->uk_ipers,
1311 keg->uk_ppera, keg->uk_pgoff);
1314 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1317 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1318 mtx_unlock(&uma_mtx);
1323 * Zone header ctor. This initializes all fields, locks, etc.
1325 * Arguments/Returns follow uma_ctor specifications
1326 * udata Actually uma_zctor_args
1330 zone_ctor(void *mem, int size, void *udata, int flags)
1332 struct uma_zctor_args *arg = udata;
1333 uma_zone_t zone = mem;
1338 zone->uz_name = arg->name;
1339 zone->uz_ctor = arg->ctor;
1340 zone->uz_dtor = arg->dtor;
1341 zone->uz_init = NULL;
1342 zone->uz_fini = NULL;
1343 zone->uz_allocs = 0;
1346 zone->uz_fills = zone->uz_count = 0;
1348 if (arg->flags & UMA_ZONE_SECONDARY) {
1349 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1352 zone->uz_init = arg->uminit;
1353 zone->uz_fini = arg->fini;
1354 zone->uz_lock = &keg->uk_lock;
1357 keg->uk_flags |= UMA_ZONE_SECONDARY;
1358 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1359 if (LIST_NEXT(z, uz_link) == NULL) {
1360 LIST_INSERT_AFTER(z, zone, uz_link);
1365 mtx_unlock(&uma_mtx);
1366 } else if (arg->keg == NULL) {
1367 if (uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1368 arg->align, arg->flags) == NULL)
1371 struct uma_kctor_args karg;
1374 /* We should only be here from uma_startup() */
1375 karg.size = arg->size;
1376 karg.uminit = arg->uminit;
1377 karg.fini = arg->fini;
1378 karg.align = arg->align;
1379 karg.flags = arg->flags;
1381 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1387 zone->uz_lock = &keg->uk_lock;
1390 * Some internal zones don't have room allocated for the per cpu
1391 * caches. If we're internal, bail out here.
1393 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1394 KASSERT((keg->uk_flags & UMA_ZONE_SECONDARY) == 0,
1395 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1399 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1400 zone->uz_count = BUCKET_MAX;
1401 else if (keg->uk_ipers <= BUCKET_MAX)
1402 zone->uz_count = keg->uk_ipers;
1404 zone->uz_count = BUCKET_MAX;
1409 * Keg header dtor. This frees all data, destroys locks, frees the hash
1410 * table and removes the keg from the global list.
1412 * Arguments/Returns follow uma_dtor specifications
1416 keg_dtor(void *arg, int size, void *udata)
1420 keg = (uma_keg_t)arg;
1421 mtx_lock(&keg->uk_lock);
1422 if (keg->uk_free != 0) {
1423 printf("Freed UMA keg was not empty (%d items). "
1424 " Lost %d pages of memory.\n",
1425 keg->uk_free, keg->uk_pages);
1427 mtx_unlock(&keg->uk_lock);
1429 if (keg->uk_flags & UMA_ZONE_HASH)
1430 hash_free(&keg->uk_hash);
1432 mtx_destroy(&keg->uk_lock);
1438 * Arguments/Returns follow uma_dtor specifications
1442 zone_dtor(void *arg, int size, void *udata)
1447 zone = (uma_zone_t)arg;
1450 if (!(keg->uk_flags & UMA_ZFLAG_INTERNAL))
1455 if (keg->uk_flags & UMA_ZONE_SECONDARY) {
1456 LIST_REMOVE(zone, uz_link);
1458 * XXX there are some races here where
1459 * the zone can be drained but zone lock
1460 * released and then refilled before we
1461 * remove it... we dont care for now
1464 if (LIST_EMPTY(&keg->uk_zones))
1465 keg->uk_flags &= ~UMA_ZONE_SECONDARY;
1467 mtx_unlock(&uma_mtx);
1469 LIST_REMOVE(keg, uk_link);
1470 LIST_REMOVE(zone, uz_link);
1471 mtx_unlock(&uma_mtx);
1472 uma_zfree_internal(kegs, keg, NULL, SKIP_NONE,
1475 zone->uz_keg = NULL;
1479 * Traverses every zone in the system and calls a callback
1482 * zfunc A pointer to a function which accepts a zone
1489 zone_foreach(void (*zfunc)(uma_zone_t))
1495 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1496 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1499 mtx_unlock(&uma_mtx);
1502 /* Public functions */
1505 uma_startup(void *bootmem, int boot_pages)
1507 struct uma_zctor_args args;
1510 u_int objsize, totsize, wsize;
1514 printf("Creating uma keg headers zone and keg.\n");
1516 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1519 * Figure out the maximum number of items-per-slab we'll have if
1520 * we're using the OFFPAGE slab header to track free items, given
1521 * all possible object sizes and the maximum desired wastage
1524 * We iterate until we find an object size for
1525 * which the calculated wastage in zone_small_init() will be
1526 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1527 * is an overall increasing see-saw function, we find the smallest
1528 * objsize such that the wastage is always acceptable for objects
1529 * with that objsize or smaller. Since a smaller objsize always
1530 * generates a larger possible uma_max_ipers, we use this computed
1531 * objsize to calculate the largest ipers possible. Since the
1532 * ipers calculated for OFFPAGE slab headers is always larger than
1533 * the ipers initially calculated in zone_small_init(), we use
1534 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1535 * obtain the maximum ipers possible for offpage slab headers.
1537 * It should be noted that ipers versus objsize is an inversly
1538 * proportional function which drops off rather quickly so as
1539 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1540 * falls into the portion of the inverse relation AFTER the steep
1541 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1543 * Note that we have 8-bits (1 byte) to use as a freelist index
1544 * inside the actual slab header itself and this is enough to
1545 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1546 * object with offpage slab header would have ipers =
1547 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1548 * 1 greater than what our byte-integer freelist index can
1549 * accomodate, but we know that this situation never occurs as
1550 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1551 * that we need to go to offpage slab headers. Or, if we do,
1552 * then we trap that condition below and panic in the INVARIANTS case.
1554 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1556 objsize = UMA_SMALLEST_UNIT;
1557 while (totsize >= wsize) {
1558 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1559 (objsize + UMA_FRITM_SZ);
1560 totsize *= (UMA_FRITM_SZ + objsize);
1563 if (objsize > UMA_SMALLEST_UNIT)
1565 uma_max_ipers = UMA_SLAB_SIZE / objsize;
1567 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1569 objsize = UMA_SMALLEST_UNIT;
1570 while (totsize >= wsize) {
1571 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1572 (objsize + UMA_FRITMREF_SZ);
1573 totsize *= (UMA_FRITMREF_SZ + objsize);
1576 if (objsize > UMA_SMALLEST_UNIT)
1578 uma_max_ipers_ref = UMA_SLAB_SIZE / objsize;
1580 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1581 ("uma_startup: calculated uma_max_ipers values too large!"));
1584 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1585 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1589 /* "manually" create the initial zone */
1590 args.name = "UMA Kegs";
1591 args.size = sizeof(struct uma_keg);
1592 args.ctor = keg_ctor;
1593 args.dtor = keg_dtor;
1594 args.uminit = zero_init;
1596 args.keg = &masterkeg;
1597 args.align = 32 - 1;
1598 args.flags = UMA_ZFLAG_INTERNAL;
1599 /* The initial zone has no Per cpu queues so it's smaller */
1600 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1603 printf("Filling boot free list.\n");
1605 for (i = 0; i < boot_pages; i++) {
1606 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1607 slab->us_data = (u_int8_t *)slab;
1608 slab->us_flags = UMA_SLAB_BOOT;
1609 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1611 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1614 printf("Creating uma zone headers zone and keg.\n");
1616 args.name = "UMA Zones";
1617 args.size = sizeof(struct uma_zone) +
1618 (sizeof(struct uma_cache) * (mp_maxid + 1));
1619 args.ctor = zone_ctor;
1620 args.dtor = zone_dtor;
1621 args.uminit = zero_init;
1624 args.align = 32 - 1;
1625 args.flags = UMA_ZFLAG_INTERNAL;
1626 /* The initial zone has no Per cpu queues so it's smaller */
1627 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1630 printf("Initializing pcpu cache locks.\n");
1633 printf("Creating slab and hash zones.\n");
1637 * This is the max number of free list items we'll have with
1640 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1641 slabsize += sizeof(struct uma_slab);
1643 /* Now make a zone for slab headers */
1644 slabzone = uma_zcreate("UMA Slabs",
1646 NULL, NULL, NULL, NULL,
1647 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1650 * We also create a zone for the bigger slabs with reference
1651 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1653 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1654 slabsize += sizeof(struct uma_slab_refcnt);
1655 slabrefzone = uma_zcreate("UMA RCntSlabs",
1657 NULL, NULL, NULL, NULL,
1659 UMA_ZFLAG_INTERNAL);
1661 hashzone = uma_zcreate("UMA Hash",
1662 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1663 NULL, NULL, NULL, NULL,
1664 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1668 #ifdef UMA_MD_SMALL_ALLOC
1673 printf("UMA startup complete.\n");
1684 printf("UMA startup2 complete.\n");
1689 * Initialize our callout handle
1697 printf("Starting callout.\n");
1699 callout_init(&uma_callout, CALLOUT_MPSAFE);
1700 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1702 printf("UMA startup3 complete.\n");
1707 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1708 int align, u_int32_t flags)
1710 struct uma_kctor_args args;
1713 args.uminit = uminit;
1718 return (uma_zalloc_internal(kegs, &args, M_WAITOK));
1723 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1724 uma_init uminit, uma_fini fini, int align, u_int32_t flags)
1727 struct uma_zctor_args args;
1729 /* This stuff is essential for the zone ctor */
1734 args.uminit = uminit;
1740 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1745 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1746 uma_init zinit, uma_fini zfini, uma_zone_t master)
1748 struct uma_zctor_args args;
1751 args.size = master->uz_keg->uk_size;
1754 args.uminit = zinit;
1756 args.align = master->uz_keg->uk_align;
1757 args.flags = master->uz_keg->uk_flags | UMA_ZONE_SECONDARY;
1758 args.keg = master->uz_keg;
1760 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1765 uma_zdestroy(uma_zone_t zone)
1768 uma_zfree_internal(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1773 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1777 uma_bucket_t bucket;
1781 /* This is the fast path allocation */
1782 #ifdef UMA_DEBUG_ALLOC_1
1783 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1785 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1786 zone->uz_name, flags);
1788 if (!(flags & M_NOWAIT)) {
1789 KASSERT(curthread->td_intr_nesting_level == 0,
1790 ("malloc(M_WAITOK) in interrupt context"));
1791 if (nosleepwithlocks) {
1793 badness = WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
1795 "malloc(M_WAITOK) of \"%s\", forcing M_NOWAIT",
1803 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1804 "malloc(M_WAITOK) of \"%s\"", zone->uz_name);
1814 * If possible, allocate from the per-CPU cache. There are two
1815 * requirements for safe access to the per-CPU cache: (1) the thread
1816 * accessing the cache must not be preempted or yield during access,
1817 * and (2) the thread must not migrate CPUs without switching which
1818 * cache it accesses. We rely on a critical section to prevent
1819 * preemption and migration. We release the critical section in
1820 * order to acquire the zone mutex if we are unable to allocate from
1821 * the current cache; when we re-acquire the critical section, we
1822 * must detect and handle migration if it has occurred.
1827 cache = &zone->uz_cpu[cpu];
1830 bucket = cache->uc_allocbucket;
1833 if (bucket->ub_cnt > 0) {
1835 item = bucket->ub_bucket[bucket->ub_cnt];
1837 bucket->ub_bucket[bucket->ub_cnt] = NULL;
1839 KASSERT(item != NULL,
1840 ("uma_zalloc: Bucket pointer mangled."));
1845 uma_dbg_alloc(zone, NULL, item);
1848 if (zone->uz_ctor != NULL) {
1849 if (zone->uz_ctor(item, zone->uz_keg->uk_size,
1850 udata, flags) != 0) {
1851 uma_zfree_internal(zone, item, udata,
1852 SKIP_DTOR, ZFREE_STATFAIL |
1858 bzero(item, zone->uz_keg->uk_size);
1860 } else if (cache->uc_freebucket) {
1862 * We have run out of items in our allocbucket.
1863 * See if we can switch with our free bucket.
1865 if (cache->uc_freebucket->ub_cnt > 0) {
1866 #ifdef UMA_DEBUG_ALLOC
1867 printf("uma_zalloc: Swapping empty with"
1870 bucket = cache->uc_freebucket;
1871 cache->uc_freebucket = cache->uc_allocbucket;
1872 cache->uc_allocbucket = bucket;
1879 * Attempt to retrieve the item from the per-CPU cache has failed, so
1880 * we must go back to the zone. This requires the zone lock, so we
1881 * must drop the critical section, then re-acquire it when we go back
1882 * to the cache. Since the critical section is released, we may be
1883 * preempted or migrate. As such, make sure not to maintain any
1884 * thread-local state specific to the cache from prior to releasing
1885 * the critical section.
1891 cache = &zone->uz_cpu[cpu];
1892 bucket = cache->uc_allocbucket;
1893 if (bucket != NULL) {
1894 if (bucket->ub_cnt > 0) {
1898 bucket = cache->uc_freebucket;
1899 if (bucket != NULL && bucket->ub_cnt > 0) {
1905 /* Since we have locked the zone we may as well send back our stats */
1906 zone->uz_allocs += cache->uc_allocs;
1907 cache->uc_allocs = 0;
1908 zone->uz_frees += cache->uc_frees;
1909 cache->uc_frees = 0;
1911 /* Our old one is now a free bucket */
1912 if (cache->uc_allocbucket) {
1913 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
1914 ("uma_zalloc_arg: Freeing a non free bucket."));
1915 LIST_INSERT_HEAD(&zone->uz_free_bucket,
1916 cache->uc_allocbucket, ub_link);
1917 cache->uc_allocbucket = NULL;
1920 /* Check the free list for a new alloc bucket */
1921 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
1922 KASSERT(bucket->ub_cnt != 0,
1923 ("uma_zalloc_arg: Returning an empty bucket."));
1925 LIST_REMOVE(bucket, ub_link);
1926 cache->uc_allocbucket = bucket;
1930 /* We are no longer associated with this CPU. */
1933 /* Bump up our uz_count so we get here less */
1934 if (zone->uz_count < BUCKET_MAX)
1938 * Now lets just fill a bucket and put it on the free list. If that
1939 * works we'll restart the allocation from the begining.
1941 if (uma_zalloc_bucket(zone, flags)) {
1943 goto zalloc_restart;
1947 * We may not be able to get a bucket so return an actual item.
1950 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
1953 return (uma_zalloc_internal(zone, udata, flags));
1957 uma_zone_slab(uma_zone_t zone, int flags)
1965 * This is to prevent us from recursively trying to allocate
1966 * buckets. The problem is that if an allocation forces us to
1967 * grab a new bucket we will call page_alloc, which will go off
1968 * and cause the vm to allocate vm_map_entries. If we need new
1969 * buckets there too we will recurse in kmem_alloc and bad
1970 * things happen. So instead we return a NULL bucket, and make
1971 * the code that allocates buckets smart enough to deal with it
1973 * XXX: While we want this protection for the bucket zones so that
1974 * recursion from the VM is handled (and the calling code that
1975 * allocates buckets knows how to deal with it), we do not want
1976 * to prevent allocation from the slab header zones (slabzone
1977 * and slabrefzone) if uk_recurse is not zero for them. The
1978 * reason is that it could lead to NULL being returned for
1979 * slab header allocations even in the M_WAITOK case, and the
1980 * caller can't handle that.
1982 if (keg->uk_flags & UMA_ZFLAG_INTERNAL && keg->uk_recurse != 0)
1983 if ((zone != slabzone) && (zone != slabrefzone))
1990 * Find a slab with some space. Prefer slabs that are partially
1991 * used over those that are totally full. This helps to reduce
1994 if (keg->uk_free != 0) {
1995 if (!LIST_EMPTY(&keg->uk_part_slab)) {
1996 slab = LIST_FIRST(&keg->uk_part_slab);
1998 slab = LIST_FIRST(&keg->uk_free_slab);
1999 LIST_REMOVE(slab, us_link);
2000 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2007 * M_NOVM means don't ask at all!
2012 if (keg->uk_maxpages &&
2013 keg->uk_pages >= keg->uk_maxpages) {
2014 keg->uk_flags |= UMA_ZFLAG_FULL;
2016 if (flags & M_NOWAIT)
2019 msleep(keg, &keg->uk_lock, PVM,
2024 slab = slab_zalloc(zone, flags);
2028 * If we got a slab here it's safe to mark it partially used
2029 * and return. We assume that the caller is going to remove
2030 * at least one item.
2033 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2037 * We might not have been able to get a slab but another cpu
2038 * could have while we were unlocked. Check again before we
2041 if (flags & M_NOWAIT)
2048 uma_slab_alloc(uma_zone_t zone, uma_slab_t slab)
2051 uma_slabrefcnt_t slabref;
2057 freei = slab->us_firstfree;
2058 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2059 slabref = (uma_slabrefcnt_t)slab;
2060 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2062 slab->us_firstfree = slab->us_freelist[freei].us_item;
2064 item = slab->us_data + (keg->uk_rsize * freei);
2066 slab->us_freecount--;
2069 uma_dbg_alloc(zone, slab, item);
2071 /* Move this slab to the full list */
2072 if (slab->us_freecount == 0) {
2073 LIST_REMOVE(slab, us_link);
2074 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2081 uma_zalloc_bucket(uma_zone_t zone, int flags)
2083 uma_bucket_t bucket;
2086 int max, origflags = flags;
2089 * Try this zone's free list first so we don't allocate extra buckets.
2091 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2092 KASSERT(bucket->ub_cnt == 0,
2093 ("uma_zalloc_bucket: Bucket on free list is not empty."));
2094 LIST_REMOVE(bucket, ub_link);
2098 bflags = (flags & ~M_ZERO);
2099 if (zone->uz_keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2103 bucket = bucket_alloc(zone->uz_count, bflags);
2112 * This code is here to limit the number of simultaneous bucket fills
2113 * for any given zone to the number of per cpu caches in this zone. This
2114 * is done so that we don't allocate more memory than we really need.
2116 if (zone->uz_fills >= mp_ncpus)
2122 max = MIN(bucket->ub_entries, zone->uz_count);
2123 /* Try to keep the buckets totally full */
2124 saved = bucket->ub_cnt;
2125 while (bucket->ub_cnt < max &&
2126 (slab = uma_zone_slab(zone, flags)) != NULL) {
2127 while (slab->us_freecount && bucket->ub_cnt < max) {
2128 bucket->ub_bucket[bucket->ub_cnt++] =
2129 uma_slab_alloc(zone, slab);
2132 /* Don't block on the next fill */
2137 * We unlock here because we need to call the zone's init.
2138 * It should be safe to unlock because the slab dealt with
2139 * above is already on the appropriate list within the keg
2140 * and the bucket we filled is not yet on any list, so we
2143 if (zone->uz_init != NULL) {
2147 for (i = saved; i < bucket->ub_cnt; i++)
2148 if (zone->uz_init(bucket->ub_bucket[i],
2149 zone->uz_keg->uk_size, origflags) != 0)
2152 * If we couldn't initialize the whole bucket, put the
2153 * rest back onto the freelist.
2155 if (i != bucket->ub_cnt) {
2158 for (j = i; j < bucket->ub_cnt; j++) {
2159 uma_zfree_internal(zone, bucket->ub_bucket[j],
2160 NULL, SKIP_FINI, 0);
2162 bucket->ub_bucket[j] = NULL;
2171 if (bucket->ub_cnt != 0) {
2172 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2179 bucket_free(bucket);
2184 * Allocates an item for an internal zone
2187 * zone The zone to alloc for.
2188 * udata The data to be passed to the constructor.
2189 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2192 * NULL if there is no memory and M_NOWAIT is set
2193 * An item if successful
2197 uma_zalloc_internal(uma_zone_t zone, void *udata, int flags)
2206 #ifdef UMA_DEBUG_ALLOC
2207 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2211 slab = uma_zone_slab(zone, flags);
2218 item = uma_slab_alloc(zone, slab);
2225 * We have to call both the zone's init (not the keg's init)
2226 * and the zone's ctor. This is because the item is going from
2227 * a keg slab directly to the user, and the user is expecting it
2228 * to be both zone-init'd as well as zone-ctor'd.
2230 if (zone->uz_init != NULL) {
2231 if (zone->uz_init(item, keg->uk_size, flags) != 0) {
2232 uma_zfree_internal(zone, item, udata, SKIP_FINI,
2233 ZFREE_STATFAIL | ZFREE_STATFREE);
2237 if (zone->uz_ctor != NULL) {
2238 if (zone->uz_ctor(item, keg->uk_size, udata, flags) != 0) {
2239 uma_zfree_internal(zone, item, udata, SKIP_DTOR,
2240 ZFREE_STATFAIL | ZFREE_STATFREE);
2245 bzero(item, keg->uk_size);
2252 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2256 uma_bucket_t bucket;
2262 #ifdef UMA_DEBUG_ALLOC_1
2263 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2265 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2269 zone->uz_dtor(item, keg->uk_size, udata);
2272 if (keg->uk_flags & UMA_ZONE_MALLOC)
2273 uma_dbg_free(zone, udata, item);
2275 uma_dbg_free(zone, NULL, item);
2279 * The race here is acceptable. If we miss it we'll just have to wait
2280 * a little longer for the limits to be reset.
2282 if (keg->uk_flags & UMA_ZFLAG_FULL)
2283 goto zfree_internal;
2286 * If possible, free to the per-CPU cache. There are two
2287 * requirements for safe access to the per-CPU cache: (1) the thread
2288 * accessing the cache must not be preempted or yield during access,
2289 * and (2) the thread must not migrate CPUs without switching which
2290 * cache it accesses. We rely on a critical section to prevent
2291 * preemption and migration. We release the critical section in
2292 * order to acquire the zone mutex if we are unable to free to the
2293 * current cache; when we re-acquire the critical section, we must
2294 * detect and handle migration if it has occurred.
2299 cache = &zone->uz_cpu[cpu];
2302 bucket = cache->uc_freebucket;
2306 * Do we have room in our bucket? It is OK for this uz count
2307 * check to be slightly out of sync.
2310 if (bucket->ub_cnt < bucket->ub_entries) {
2311 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2312 ("uma_zfree: Freeing to non free bucket index."));
2313 bucket->ub_bucket[bucket->ub_cnt] = item;
2318 } else if (cache->uc_allocbucket) {
2319 #ifdef UMA_DEBUG_ALLOC
2320 printf("uma_zfree: Swapping buckets.\n");
2323 * We have run out of space in our freebucket.
2324 * See if we can switch with our alloc bucket.
2326 if (cache->uc_allocbucket->ub_cnt <
2327 cache->uc_freebucket->ub_cnt) {
2328 bucket = cache->uc_freebucket;
2329 cache->uc_freebucket = cache->uc_allocbucket;
2330 cache->uc_allocbucket = bucket;
2336 * We can get here for two reasons:
2338 * 1) The buckets are NULL
2339 * 2) The alloc and free buckets are both somewhat full.
2341 * We must go back the zone, which requires acquiring the zone lock,
2342 * which in turn means we must release and re-acquire the critical
2343 * section. Since the critical section is released, we may be
2344 * preempted or migrate. As such, make sure not to maintain any
2345 * thread-local state specific to the cache from prior to releasing
2346 * the critical section.
2352 cache = &zone->uz_cpu[cpu];
2353 if (cache->uc_freebucket != NULL) {
2354 if (cache->uc_freebucket->ub_cnt <
2355 cache->uc_freebucket->ub_entries) {
2359 if (cache->uc_allocbucket != NULL &&
2360 (cache->uc_allocbucket->ub_cnt <
2361 cache->uc_freebucket->ub_cnt)) {
2367 /* Since we have locked the zone we may as well send back our stats */
2368 zone->uz_allocs += cache->uc_allocs;
2369 cache->uc_allocs = 0;
2370 zone->uz_frees += cache->uc_frees;
2371 cache->uc_frees = 0;
2373 bucket = cache->uc_freebucket;
2374 cache->uc_freebucket = NULL;
2376 /* Can we throw this on the zone full list? */
2377 if (bucket != NULL) {
2378 #ifdef UMA_DEBUG_ALLOC
2379 printf("uma_zfree: Putting old bucket on the free list.\n");
2381 /* ub_cnt is pointing to the last free item */
2382 KASSERT(bucket->ub_cnt != 0,
2383 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2384 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2387 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2388 LIST_REMOVE(bucket, ub_link);
2390 cache->uc_freebucket = bucket;
2393 /* We are no longer associated with this CPU. */
2396 /* And the zone.. */
2399 #ifdef UMA_DEBUG_ALLOC
2400 printf("uma_zfree: Allocating new free bucket.\n");
2404 if (keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2406 bucket = bucket_alloc(zone->uz_count, bflags);
2409 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2416 * If nothing else caught this, we'll just do an internal free.
2419 uma_zfree_internal(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2425 * Frees an item to an INTERNAL zone or allocates a free bucket
2428 * zone The zone to free to
2429 * item The item we're freeing
2430 * udata User supplied data for the dtor
2431 * skip Skip dtors and finis
2434 uma_zfree_internal(uma_zone_t zone, void *item, void *udata,
2435 enum zfreeskip skip, int flags)
2438 uma_slabrefcnt_t slabref;
2445 if (skip < SKIP_DTOR && zone->uz_dtor)
2446 zone->uz_dtor(item, keg->uk_size, udata);
2447 if (skip < SKIP_FINI && zone->uz_fini)
2448 zone->uz_fini(item, keg->uk_size);
2452 if (flags & ZFREE_STATFAIL)
2454 if (flags & ZFREE_STATFREE)
2457 if (!(keg->uk_flags & UMA_ZONE_MALLOC)) {
2458 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2459 if (keg->uk_flags & UMA_ZONE_HASH)
2460 slab = hash_sfind(&keg->uk_hash, mem);
2462 mem += keg->uk_pgoff;
2463 slab = (uma_slab_t)mem;
2466 slab = (uma_slab_t)udata;
2469 /* Do we need to remove from any lists? */
2470 if (slab->us_freecount+1 == keg->uk_ipers) {
2471 LIST_REMOVE(slab, us_link);
2472 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2473 } else if (slab->us_freecount == 0) {
2474 LIST_REMOVE(slab, us_link);
2475 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2478 /* Slab management stuff */
2479 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2484 uma_dbg_free(zone, slab, item);
2487 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2488 slabref = (uma_slabrefcnt_t)slab;
2489 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2491 slab->us_freelist[freei].us_item = slab->us_firstfree;
2493 slab->us_firstfree = freei;
2494 slab->us_freecount++;
2496 /* Zone statistics */
2499 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2500 if (keg->uk_pages < keg->uk_maxpages)
2501 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2504 * We can handle one more allocation. Since we're clearing ZFLAG_FULL,
2505 * wake up all procs blocked on pages. This should be uncommon, so
2506 * keeping this simple for now (rather than adding count of blocked
2517 uma_zone_set_max(uma_zone_t zone, int nitems)
2523 if (keg->uk_ppera > 1)
2524 keg->uk_maxpages = nitems * keg->uk_ppera;
2526 keg->uk_maxpages = nitems / keg->uk_ipers;
2528 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2536 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2539 KASSERT(zone->uz_keg->uk_pages == 0,
2540 ("uma_zone_set_init on non-empty keg"));
2541 zone->uz_keg->uk_init = uminit;
2547 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2550 KASSERT(zone->uz_keg->uk_pages == 0,
2551 ("uma_zone_set_fini on non-empty keg"));
2552 zone->uz_keg->uk_fini = fini;
2558 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2561 KASSERT(zone->uz_keg->uk_pages == 0,
2562 ("uma_zone_set_zinit on non-empty keg"));
2563 zone->uz_init = zinit;
2569 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2572 KASSERT(zone->uz_keg->uk_pages == 0,
2573 ("uma_zone_set_zfini on non-empty keg"));
2574 zone->uz_fini = zfini;
2579 /* XXX uk_freef is not actually used with the zone locked */
2581 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2584 zone->uz_keg->uk_freef = freef;
2589 /* XXX uk_allocf is not actually used with the zone locked */
2591 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2594 zone->uz_keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2595 zone->uz_keg->uk_allocf = allocf;
2601 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2608 pages = count / keg->uk_ipers;
2610 if (pages * keg->uk_ipers < count)
2613 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2618 obj = vm_object_allocate(OBJT_DEFAULT,
2621 VM_OBJECT_LOCK_INIT(obj, "uma object");
2622 _vm_object_allocate(OBJT_DEFAULT,
2628 keg->uk_maxpages = pages;
2629 keg->uk_allocf = obj_alloc;
2630 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2637 uma_prealloc(uma_zone_t zone, int items)
2645 slabs = items / keg->uk_ipers;
2646 if (slabs * keg->uk_ipers < items)
2649 slab = slab_zalloc(zone, M_WAITOK);
2650 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2658 uma_find_refcnt(uma_zone_t zone, void *item)
2660 uma_slabrefcnt_t slabref;
2666 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
2668 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
2669 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
2670 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
2672 refcnt = &slabref->us_freelist[idx].us_refcnt;
2681 printf("UMA: vm asked us to release pages!\n");
2684 zone_foreach(zone_drain);
2686 * Some slabs may have been freed but this zone will be visited early
2687 * we visit again so that we can free pages that are empty once other
2688 * zones are drained. We have to do the same for buckets.
2690 zone_drain(slabzone);
2691 zone_drain(slabrefzone);
2692 bucket_zone_drain();
2697 uma_zone_exhausted(uma_zone_t zone)
2702 full = (zone->uz_keg->uk_flags & UMA_ZFLAG_FULL);
2708 uma_zone_exhausted_nolock(uma_zone_t zone)
2710 return (zone->uz_keg->uk_flags & UMA_ZFLAG_FULL);
2714 uma_large_malloc(int size, int wait)
2720 slab = uma_zalloc_internal(slabzone, NULL, wait);
2723 mem = page_alloc(NULL, size, &flags, wait);
2725 vsetslab((vm_offset_t)mem, slab);
2726 slab->us_data = mem;
2727 slab->us_flags = flags | UMA_SLAB_MALLOC;
2728 slab->us_size = size;
2730 uma_zfree_internal(slabzone, slab, NULL, SKIP_NONE,
2731 ZFREE_STATFAIL | ZFREE_STATFREE);
2738 uma_large_free(uma_slab_t slab)
2740 vsetobj((vm_offset_t)slab->us_data, kmem_object);
2741 page_free(slab->us_data, slab->us_size, slab->us_flags);
2742 uma_zfree_internal(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
2746 uma_print_stats(void)
2748 zone_foreach(uma_print_zone);
2752 slab_print(uma_slab_t slab)
2754 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
2755 slab->us_keg, slab->us_data, slab->us_freecount,
2756 slab->us_firstfree);
2760 cache_print(uma_cache_t cache)
2762 printf("alloc: %p(%d), free: %p(%d)\n",
2763 cache->uc_allocbucket,
2764 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
2765 cache->uc_freebucket,
2766 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
2770 uma_print_zone(uma_zone_t zone)
2778 printf("%s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
2779 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
2780 keg->uk_ipers, keg->uk_ppera,
2781 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
2782 printf("Part slabs:\n");
2783 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
2785 printf("Free slabs:\n");
2786 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
2788 printf("Full slabs:\n");
2789 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
2791 for (i = 0; i <= mp_maxid; i++) {
2794 cache = &zone->uz_cpu[i];
2795 printf("CPU %d Cache:\n", i);
2801 * Generate statistics across both the zone and its per-cpu cache's. Return
2802 * desired statistics if the pointer is non-NULL for that statistic.
2804 * Note: does not update the zone statistics, as it can't safely clear the
2805 * per-CPU cache statistic.
2807 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
2808 * safe from off-CPU; we should modify the caches to track this information
2809 * directly so that we don't have to.
2812 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
2816 u_int64_t allocs, frees;
2821 for (cpu = 0; cpu <= mp_maxid; cpu++) {
2822 if (CPU_ABSENT(cpu))
2824 cache = &z->uz_cpu[cpu];
2825 if (cache->uc_allocbucket != NULL)
2826 cachefree += cache->uc_allocbucket->ub_cnt;
2827 if (cache->uc_freebucket != NULL)
2828 cachefree += cache->uc_freebucket->ub_cnt;
2829 allocs += cache->uc_allocs;
2830 frees += cache->uc_frees;
2832 allocs += z->uz_allocs;
2833 frees += z->uz_frees;
2834 if (cachefreep != NULL)
2835 *cachefreep = cachefree;
2836 if (allocsp != NULL)
2843 * Sysctl handler for vm.zone
2845 * stolen from vm_zone.c
2848 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
2850 int error, len, cnt;
2851 const int linesize = 128; /* conservative */
2853 char *tmpbuf, *offset;
2858 uma_bucket_t bucket;
2859 u_int64_t allocs, frees;
2863 LIST_FOREACH(zk, &uma_kegs, uk_link) {
2864 LIST_FOREACH(z, &zk->uk_zones, uz_link)
2867 mtx_unlock(&uma_mtx);
2868 MALLOC(tmpbuf, char *, (cnt == 0 ? 1 : cnt) * linesize,
2870 len = snprintf(tmpbuf, linesize,
2871 "\nITEM SIZE LIMIT USED FREE REQUESTS\n\n");
2873 tmpbuf[len - 1] = '\0';
2874 error = SYSCTL_OUT(req, tmpbuf, cnt == 0 ? len-1 : len);
2875 if (error || cnt == 0)
2879 LIST_FOREACH(zk, &uma_kegs, uk_link) {
2880 LIST_FOREACH(z, &zk->uk_zones, uz_link) {
2881 if (cnt == 0) /* list may have changed size */
2885 if (!(zk->uk_flags & UMA_ZFLAG_INTERNAL)) {
2886 uma_zone_sumstat(z, &cachefree, &allocs, &frees);
2888 allocs = z->uz_allocs;
2889 frees = z->uz_frees;
2892 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link) {
2893 cachefree += bucket->ub_cnt;
2895 totalfree = zk->uk_free + cachefree;
2896 len = snprintf(offset, linesize,
2897 "%-12.12s %6.6u, %8.8u, %6.6u, %6.6u, %8.8llu\n",
2898 z->uz_name, zk->uk_size,
2899 zk->uk_maxpages * zk->uk_ipers,
2900 (zk->uk_ipers * (zk->uk_pages / zk->uk_ppera)) - totalfree,
2902 (unsigned long long)allocs);
2904 for (p = offset + 12; p > offset && *p == ' '; --p)
2911 mtx_unlock(&uma_mtx);
2913 error = SYSCTL_OUT(req, tmpbuf, offset - tmpbuf);
2915 FREE(tmpbuf, M_TEMP);
2920 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
2928 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2929 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2932 mtx_unlock(&uma_mtx);
2933 return (sysctl_handle_int(oidp, &count, 0, req));
2937 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
2939 struct uma_stream_header ush;
2940 struct uma_type_header uth;
2941 struct uma_percpu_stat ups;
2942 uma_bucket_t bucket;
2948 int buflen, count, error, i;
2952 mtx_assert(&uma_mtx, MA_OWNED);
2954 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2955 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2958 mtx_unlock(&uma_mtx);
2960 buflen = sizeof(ush) + count * (sizeof(uth) + sizeof(ups) *
2961 (mp_maxid + 1)) + 1;
2962 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
2966 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2967 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2971 free(buffer, M_TEMP);
2976 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
2979 * Insert stream header.
2981 bzero(&ush, sizeof(ush));
2982 ush.ush_version = UMA_STREAM_VERSION;
2983 ush.ush_maxcpus = (mp_maxid + 1);
2984 ush.ush_count = count;
2985 if (sbuf_bcat(&sbuf, &ush, sizeof(ush)) < 0) {
2986 mtx_unlock(&uma_mtx);
2991 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2992 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
2993 bzero(&uth, sizeof(uth));
2995 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
2996 uth.uth_align = kz->uk_align;
2997 uth.uth_pages = kz->uk_pages;
2998 uth.uth_keg_free = kz->uk_free;
2999 uth.uth_size = kz->uk_size;
3000 uth.uth_rsize = kz->uk_rsize;
3001 uth.uth_maxpages = kz->uk_maxpages;
3002 if (kz->uk_ppera > 1)
3003 uth.uth_limit = kz->uk_maxpages /
3006 uth.uth_limit = kz->uk_maxpages *
3010 * A zone is secondary is it is not the first entry
3011 * on the keg's zone list.
3013 if ((kz->uk_flags & UMA_ZONE_SECONDARY) &&
3014 (LIST_FIRST(&kz->uk_zones) != z))
3015 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3017 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3018 uth.uth_zone_free += bucket->ub_cnt;
3019 uth.uth_allocs = z->uz_allocs;
3020 uth.uth_frees = z->uz_frees;
3021 uth.uth_fails = z->uz_fails;
3022 if (sbuf_bcat(&sbuf, &uth, sizeof(uth)) < 0) {
3024 mtx_unlock(&uma_mtx);
3029 * While it is not normally safe to access the cache
3030 * bucket pointers while not on the CPU that owns the
3031 * cache, we only allow the pointers to be exchanged
3032 * without the zone lock held, not invalidated, so
3033 * accept the possible race associated with bucket
3034 * exchange during monitoring.
3036 for (i = 0; i < (mp_maxid + 1); i++) {
3037 bzero(&ups, sizeof(ups));
3038 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3042 cache = &z->uz_cpu[i];
3043 if (cache->uc_allocbucket != NULL)
3044 ups.ups_cache_free +=
3045 cache->uc_allocbucket->ub_cnt;
3046 if (cache->uc_freebucket != NULL)
3047 ups.ups_cache_free +=
3048 cache->uc_freebucket->ub_cnt;
3049 ups.ups_allocs = cache->uc_allocs;
3050 ups.ups_frees = cache->uc_frees;
3052 if (sbuf_bcat(&sbuf, &ups, sizeof(ups)) < 0) {
3054 mtx_unlock(&uma_mtx);
3062 mtx_unlock(&uma_mtx);
3064 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
3066 free(buffer, M_TEMP);
3071 DB_SHOW_COMMAND(uma, db_show_uma)
3073 u_int64_t allocs, frees;
3074 uma_bucket_t bucket;
3079 db_printf("%18s %12s %12s %12s %8s\n", "Zone", "Allocs", "Frees",
3081 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3082 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3083 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3084 allocs = z->uz_allocs;
3085 frees = z->uz_frees;
3088 uma_zone_sumstat(z, &cachefree, &allocs,
3090 if (!((kz->uk_flags & UMA_ZONE_SECONDARY) &&
3091 (LIST_FIRST(&kz->uk_zones) != z)))
3092 cachefree += kz->uk_free;
3093 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3094 cachefree += bucket->ub_cnt;
3095 db_printf("%18s %12ju %12ju %12ju %8d\n", z->uz_name,
3096 allocs, frees, allocs - frees, cachefree);