2 * Copyright (c) 2004-2005 Robert N. M. Watson
3 * Copyright (c) 2004, 2005,
4 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved.
5 * Copyright (c) 2002, 2003, 2004, 2005,
6 * Jeffrey Roberson <jeff@FreeBSD.org>. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * uma_core.c Implementation of the Universal Memory allocator
33 * This allocator is intended to replace the multitude of similar object caches
34 * in the standard FreeBSD kernel. The intent is to be flexible as well as
35 * effecient. A primary design goal is to return unused memory to the rest of
36 * the system. This will make the system as a whole more flexible due to the
37 * ability to move memory to subsystems which most need it instead of leaving
38 * pools of reserved memory unused.
40 * The basic ideas stem from similar slab/zone based allocators whose algorithms
47 * - Improve memory usage for large allocations
48 * - Investigate cache size adjustments
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
54 /* I should really use ktr.. */
57 #define UMA_DEBUG_ALLOC 1
58 #define UMA_DEBUG_ALLOC_1 1
61 #include "opt_param.h"
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/types.h>
66 #include <sys/queue.h>
67 #include <sys/malloc.h>
70 #include <sys/sysctl.h>
71 #include <sys/mutex.h>
75 #include <sys/vmmeter.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_param.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_kern.h>
83 #include <vm/vm_extern.h>
85 #include <vm/uma_int.h>
86 #include <vm/uma_dbg.h>
88 #include <machine/vmparam.h>
91 * This is the zone and keg from which all zones are spawned. The idea is that
92 * even the zone & keg heads are allocated from the allocator, so we use the
93 * bss section to bootstrap us.
95 static struct uma_keg masterkeg;
96 static struct uma_zone masterzone_k;
97 static struct uma_zone masterzone_z;
98 static uma_zone_t kegs = &masterzone_k;
99 static uma_zone_t zones = &masterzone_z;
101 /* This is the zone from which all of uma_slab_t's are allocated. */
102 static uma_zone_t slabzone;
103 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
106 * The initial hash tables come out of this zone so they can be allocated
107 * prior to malloc coming up.
109 static uma_zone_t hashzone;
111 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
114 * Are we allowed to allocate buckets?
116 static int bucketdisable = 1;
118 /* Linked list of all kegs in the system */
119 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(&uma_kegs);
121 /* This mutex protects the keg list */
122 static struct mtx uma_mtx;
124 /* Linked list of boot time pages */
125 static LIST_HEAD(,uma_slab) uma_boot_pages =
126 LIST_HEAD_INITIALIZER(&uma_boot_pages);
128 /* Count of free boottime pages */
129 static int uma_boot_free = 0;
131 /* Is the VM done starting up? */
132 static int booted = 0;
134 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
135 static u_int uma_max_ipers;
136 static u_int uma_max_ipers_ref;
139 * This is the handle used to schedule events that need to happen
140 * outside of the allocation fast path.
142 static struct callout uma_callout;
143 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
146 * This structure is passed as the zone ctor arg so that I don't have to create
147 * a special allocation function just for zones.
149 struct uma_zctor_args {
161 struct uma_kctor_args {
170 struct uma_bucket_zone {
176 #define BUCKET_MAX 128
178 struct uma_bucket_zone bucket_zones[] = {
179 { NULL, "16 Bucket", 16 },
180 { NULL, "32 Bucket", 32 },
181 { NULL, "64 Bucket", 64 },
182 { NULL, "128 Bucket", 128 },
186 #define BUCKET_SHIFT 4
187 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
190 * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
191 * of approximately the right size.
193 static uint8_t bucket_size[BUCKET_ZONES];
195 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
199 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
200 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
201 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
202 static void page_free(void *, int, u_int8_t);
203 static uma_slab_t slab_zalloc(uma_zone_t, int);
204 static void cache_drain(uma_zone_t);
205 static void bucket_drain(uma_zone_t, uma_bucket_t);
206 static void bucket_cache_drain(uma_zone_t zone);
207 static int keg_ctor(void *, int, void *, int);
208 static void keg_dtor(void *, int, void *);
209 static int zone_ctor(void *, int, void *, int);
210 static void zone_dtor(void *, int, void *);
211 static int zero_init(void *, int, int);
212 static void zone_small_init(uma_zone_t zone);
213 static void zone_large_init(uma_zone_t zone);
214 static void zone_foreach(void (*zfunc)(uma_zone_t));
215 static void zone_timeout(uma_zone_t zone);
216 static int hash_alloc(struct uma_hash *);
217 static int hash_expand(struct uma_hash *, struct uma_hash *);
218 static void hash_free(struct uma_hash *hash);
219 static void uma_timeout(void *);
220 static void uma_startup3(void);
221 static void *uma_zalloc_internal(uma_zone_t, void *, int);
222 static void uma_zfree_internal(uma_zone_t, void *, void *, enum zfreeskip);
223 static void bucket_enable(void);
224 static void bucket_init(void);
225 static uma_bucket_t bucket_alloc(int, int);
226 static void bucket_free(uma_bucket_t);
227 static void bucket_zone_drain(void);
228 static int uma_zalloc_bucket(uma_zone_t zone, int flags);
229 static uma_slab_t uma_zone_slab(uma_zone_t zone, int flags);
230 static void *uma_slab_alloc(uma_zone_t zone, uma_slab_t slab);
231 static void zone_drain(uma_zone_t);
232 static uma_zone_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
233 uma_fini fini, int align, u_int16_t flags);
235 void uma_print_zone(uma_zone_t);
236 void uma_print_stats(void);
237 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
238 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
239 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
242 static int nosleepwithlocks = 1;
243 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
244 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
246 static int nosleepwithlocks = 0;
247 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
248 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
250 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD,
251 NULL, 0, sysctl_vm_zone, "A", "Zone Info");
252 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
254 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
255 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
257 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
258 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
261 * This routine checks to see whether or not it's safe to enable buckets.
267 if (cnt.v_free_count < cnt.v_free_min)
274 * Initialize bucket_zones, the array of zones of buckets of various sizes.
276 * For each zone, calculate the memory required for each bucket, consisting
277 * of the header and an array of pointers. Initialize bucket_size[] to point
278 * the range of appropriate bucket sizes at the zone.
283 struct uma_bucket_zone *ubz;
287 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
290 ubz = &bucket_zones[j];
291 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
292 size += sizeof(void *) * ubz->ubz_entries;
293 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
294 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
295 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
296 bucket_size[i >> BUCKET_SHIFT] = j;
301 * Given a desired number of entries for a bucket, return the zone from which
302 * to allocate the bucket.
304 static struct uma_bucket_zone *
305 bucket_zone_lookup(int entries)
309 idx = howmany(entries, 1 << BUCKET_SHIFT);
310 return (&bucket_zones[bucket_size[idx]]);
314 bucket_alloc(int entries, int bflags)
316 struct uma_bucket_zone *ubz;
320 * This is to stop us from allocating per cpu buckets while we're
321 * running out of UMA_BOOT_PAGES. Otherwise, we would exhaust the
322 * boot pages. This also prevents us from allocating buckets in
323 * low memory situations.
328 ubz = bucket_zone_lookup(entries);
329 bucket = uma_zalloc_internal(ubz->ubz_zone, NULL, bflags);
332 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
335 bucket->ub_entries = ubz->ubz_entries;
342 bucket_free(uma_bucket_t bucket)
344 struct uma_bucket_zone *ubz;
346 ubz = bucket_zone_lookup(bucket->ub_entries);
347 uma_zfree_internal(ubz->ubz_zone, bucket, NULL, SKIP_NONE);
351 bucket_zone_drain(void)
353 struct uma_bucket_zone *ubz;
355 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
356 zone_drain(ubz->ubz_zone);
361 * Routine called by timeout which is used to fire off some time interval
362 * based calculations. (stats, hash size, etc.)
371 uma_timeout(void *unused)
374 zone_foreach(zone_timeout);
376 /* Reschedule this event */
377 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
381 * Routine to perform timeout driven calculations. This expands the
382 * hashes and does per cpu statistics aggregation.
385 * zone The zone to operate on
391 zone_timeout(uma_zone_t zone)
400 * Expand the zone hash table.
402 * This is done if the number of slabs is larger than the hash size.
403 * What I'm trying to do here is completely reduce collisions. This
404 * may be a little aggressive. Should I allow for two collisions max?
407 if (keg->uk_flags & UMA_ZONE_HASH &&
408 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
409 struct uma_hash newhash;
410 struct uma_hash oldhash;
414 * This is so involved because allocating and freeing
415 * while the zone lock is held will lead to deadlock.
416 * I have to do everything in stages and check for
419 newhash = keg->uk_hash;
421 ret = hash_alloc(&newhash);
424 if (hash_expand(&keg->uk_hash, &newhash)) {
425 oldhash = keg->uk_hash;
426 keg->uk_hash = newhash;
439 * Allocate and zero fill the next sized hash table from the appropriate
443 * hash A new hash structure with the old hash size in uh_hashsize
446 * 1 on sucess and 0 on failure.
449 hash_alloc(struct uma_hash *hash)
454 oldsize = hash->uh_hashsize;
456 /* We're just going to go to a power of two greater */
458 hash->uh_hashsize = oldsize * 2;
459 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
460 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
461 M_UMAHASH, M_NOWAIT);
463 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
464 hash->uh_slab_hash = uma_zalloc_internal(hashzone, NULL,
466 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
468 if (hash->uh_slab_hash) {
469 bzero(hash->uh_slab_hash, alloc);
470 hash->uh_hashmask = hash->uh_hashsize - 1;
478 * Expands the hash table for HASH zones. This is done from zone_timeout
479 * to reduce collisions. This must not be done in the regular allocation
480 * path, otherwise, we can recurse on the vm while allocating pages.
483 * oldhash The hash you want to expand
484 * newhash The hash structure for the new table
492 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
498 if (!newhash->uh_slab_hash)
501 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
505 * I need to investigate hash algorithms for resizing without a
509 for (i = 0; i < oldhash->uh_hashsize; i++)
510 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
511 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
512 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
513 hval = UMA_HASH(newhash, slab->us_data);
514 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
522 * Free the hash bucket to the appropriate backing store.
525 * slab_hash The hash bucket we're freeing
526 * hashsize The number of entries in that hash bucket
532 hash_free(struct uma_hash *hash)
534 if (hash->uh_slab_hash == NULL)
536 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
537 uma_zfree_internal(hashzone,
538 hash->uh_slab_hash, NULL, SKIP_NONE);
540 free(hash->uh_slab_hash, M_UMAHASH);
544 * Frees all outstanding items in a bucket
547 * zone The zone to free to, must be unlocked.
548 * bucket The free/alloc bucket with items, cpu queue must be locked.
555 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
567 /* We have to lookup the slab again for malloc.. */
568 if (zone->uz_keg->uk_flags & UMA_ZONE_MALLOC)
571 while (bucket->ub_cnt > 0) {
573 item = bucket->ub_bucket[bucket->ub_cnt];
575 bucket->ub_bucket[bucket->ub_cnt] = NULL;
576 KASSERT(item != NULL,
577 ("bucket_drain: botched ptr, item is NULL"));
580 * This is extremely inefficient. The slab pointer was passed
581 * to uma_zfree_arg, but we lost it because the buckets don't
582 * hold them. This will go away when free() gets a size passed
586 slab = vtoslab((vm_offset_t)item & (~UMA_SLAB_MASK));
587 uma_zfree_internal(zone, item, slab, SKIP_DTOR);
592 * Drains the per cpu caches for a zone.
594 * NOTE: This may only be called while the zone is being turn down, and not
595 * during normal operation. This is necessary in order that we do not have
596 * to migrate CPUs to drain the per-CPU caches.
599 * zone The zone to drain, must be unlocked.
605 cache_drain(uma_zone_t zone)
611 * XXX: It is safe to not lock the per-CPU caches, because we're
612 * tearing down the zone anyway. I.e., there will be no further use
613 * of the caches at this point.
615 * XXX: It would good to be able to assert that the zone is being
616 * torn down to prevent improper use of cache_drain().
618 * XXX: We lock the zone before passing into bucket_cache_drain() as
619 * it is used elsewhere. Should the tear-down path be made special
620 * there in some form?
622 for (cpu = 0; cpu <= mp_maxid; cpu++) {
625 cache = &zone->uz_cpu[cpu];
626 bucket_drain(zone, cache->uc_allocbucket);
627 bucket_drain(zone, cache->uc_freebucket);
628 if (cache->uc_allocbucket != NULL)
629 bucket_free(cache->uc_allocbucket);
630 if (cache->uc_freebucket != NULL)
631 bucket_free(cache->uc_freebucket);
632 cache->uc_allocbucket = cache->uc_freebucket = NULL;
635 bucket_cache_drain(zone);
640 * Drain the cached buckets from a zone. Expects a locked zone on entry.
643 bucket_cache_drain(uma_zone_t zone)
648 * Drain the bucket queues and free the buckets, we just keep two per
651 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
652 LIST_REMOVE(bucket, ub_link);
654 bucket_drain(zone, bucket);
659 /* Now we do the free queue.. */
660 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
661 LIST_REMOVE(bucket, ub_link);
667 * Frees pages from a zone back to the system. This is done on demand from
668 * the pageout daemon.
671 * zone The zone to free pages from
672 * all Should we drain all items?
678 zone_drain(uma_zone_t zone)
680 struct slabhead freeslabs = { 0 };
691 * We don't want to take pages from statically allocated zones at this
694 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
700 printf("%s free items: %u\n", zone->uz_name, keg->uk_free);
702 bucket_cache_drain(zone);
703 if (keg->uk_free == 0)
706 slab = LIST_FIRST(&keg->uk_free_slab);
708 n = LIST_NEXT(slab, us_link);
710 /* We have no where to free these to */
711 if (slab->us_flags & UMA_SLAB_BOOT) {
716 LIST_REMOVE(slab, us_link);
717 keg->uk_pages -= keg->uk_ppera;
718 keg->uk_free -= keg->uk_ipers;
720 if (keg->uk_flags & UMA_ZONE_HASH)
721 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
723 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
730 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
731 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
733 for (i = 0; i < keg->uk_ipers; i++)
735 slab->us_data + (keg->uk_rsize * i),
737 flags = slab->us_flags;
740 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
741 (keg->uk_flags & UMA_ZONE_REFCNT)) {
744 if (flags & UMA_SLAB_KMEM)
748 for (i = 0; i < keg->uk_ppera; i++)
749 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
752 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
753 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
756 printf("%s: Returning %d bytes.\n",
757 zone->uz_name, UMA_SLAB_SIZE * keg->uk_ppera);
759 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
764 * Allocate a new slab for a zone. This does not insert the slab onto a list.
767 * zone The zone to allocate slabs for
768 * wait Shall we wait?
771 * The slab that was allocated or NULL if there is no memory and the
772 * caller specified M_NOWAIT.
775 slab_zalloc(uma_zone_t zone, int wait)
777 uma_slabrefcnt_t slabref;
788 printf("slab_zalloc: Allocating a new slab for %s\n", zone->uz_name);
792 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
793 slab = uma_zalloc_internal(keg->uk_slabzone, NULL, wait);
801 * This reproduces the old vm_zone behavior of zero filling pages the
802 * first time they are added to a zone.
804 * Malloced items are zeroed in uma_zalloc.
807 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
812 mem = keg->uk_allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE,
815 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
816 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
822 /* Point the slab into the allocated memory */
823 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
824 slab = (uma_slab_t )(mem + keg->uk_pgoff);
826 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
827 (keg->uk_flags & UMA_ZONE_REFCNT))
828 for (i = 0; i < keg->uk_ppera; i++)
829 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
833 slab->us_freecount = keg->uk_ipers;
834 slab->us_firstfree = 0;
835 slab->us_flags = flags;
837 if (keg->uk_flags & UMA_ZONE_REFCNT) {
838 slabref = (uma_slabrefcnt_t)slab;
839 for (i = 0; i < keg->uk_ipers; i++) {
840 slabref->us_freelist[i].us_refcnt = 0;
841 slabref->us_freelist[i].us_item = i+1;
844 for (i = 0; i < keg->uk_ipers; i++)
845 slab->us_freelist[i].us_item = i+1;
848 if (keg->uk_init != NULL) {
849 for (i = 0; i < keg->uk_ipers; i++)
850 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
851 keg->uk_size, wait) != 0)
853 if (i != keg->uk_ipers) {
854 if (keg->uk_fini != NULL) {
855 for (i--; i > -1; i--)
856 keg->uk_fini(slab->us_data +
860 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
861 (keg->uk_flags & UMA_ZONE_REFCNT)) {
864 if (flags & UMA_SLAB_KMEM)
868 for (i = 0; i < keg->uk_ppera; i++)
869 vsetobj((vm_offset_t)mem +
870 (i * PAGE_SIZE), obj);
872 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
873 uma_zfree_internal(keg->uk_slabzone, slab,
875 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
883 if (keg->uk_flags & UMA_ZONE_HASH)
884 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
886 keg->uk_pages += keg->uk_ppera;
887 keg->uk_free += keg->uk_ipers;
893 * This function is intended to be used early on in place of page_alloc() so
894 * that we may use the boot time page cache to satisfy allocations before
898 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
905 * Check our small startup cache to see if it has pages remaining.
908 if (uma_boot_free != 0) {
911 tmps = LIST_FIRST(&uma_boot_pages);
912 LIST_REMOVE(tmps, us_link);
914 mtx_unlock(&uma_mtx);
915 *pflag = tmps->us_flags;
916 return (tmps->us_data);
918 mtx_unlock(&uma_mtx);
920 panic("UMA: Increase UMA_BOOT_PAGES");
922 * Now that we've booted reset these users to their real allocator.
924 #ifdef UMA_MD_SMALL_ALLOC
925 keg->uk_allocf = uma_small_alloc;
927 keg->uk_allocf = page_alloc;
929 return keg->uk_allocf(zone, bytes, pflag, wait);
933 * Allocates a number of pages from the system
937 * bytes The number of bytes requested
938 * wait Shall we wait?
941 * A pointer to the alloced memory or possibly
942 * NULL if M_NOWAIT is set.
945 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
947 void *p; /* Returned page */
949 *pflag = UMA_SLAB_KMEM;
950 p = (void *) kmem_malloc(kmem_map, bytes, wait);
956 * Allocates a number of pages from within an object
960 * bytes The number of bytes requested
961 * wait Shall we wait?
964 * A pointer to the alloced memory or possibly
965 * NULL if M_NOWAIT is set.
968 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
971 vm_offset_t retkva, zkva;
973 int pages, startpages;
975 object = zone->uz_keg->uk_obj;
979 * This looks a little weird since we're getting one page at a time.
981 VM_OBJECT_LOCK(object);
982 p = TAILQ_LAST(&object->memq, pglist);
983 pages = p != NULL ? p->pindex + 1 : 0;
985 zkva = zone->uz_keg->uk_kva + pages * PAGE_SIZE;
986 for (; bytes > 0; bytes -= PAGE_SIZE) {
987 p = vm_page_alloc(object, pages,
988 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
990 if (pages != startpages)
991 pmap_qremove(retkva, pages - startpages);
992 while (pages != startpages) {
994 p = TAILQ_LAST(&object->memq, pglist);
995 vm_page_lock_queues();
996 vm_page_unwire(p, 0);
998 vm_page_unlock_queues();
1003 pmap_qenter(zkva, &p, 1);
1010 VM_OBJECT_UNLOCK(object);
1011 *flags = UMA_SLAB_PRIV;
1013 return ((void *)retkva);
1017 * Frees a number of pages to the system
1020 * mem A pointer to the memory to be freed
1021 * size The size of the memory being freed
1022 * flags The original p->us_flags field
1028 page_free(void *mem, int size, u_int8_t flags)
1032 if (flags & UMA_SLAB_KMEM)
1035 panic("UMA: page_free used with invalid flags %d\n", flags);
1037 kmem_free(map, (vm_offset_t)mem, size);
1041 * Zero fill initializer
1043 * Arguments/Returns follow uma_init specifications
1046 zero_init(void *mem, int size, int flags)
1053 * Finish creating a small uma zone. This calculates ipers, and the zone size.
1056 * zone The zone we should initialize
1062 zone_small_init(uma_zone_t zone)
1071 KASSERT(keg != NULL, ("Keg is null in zone_small_init"));
1072 rsize = keg->uk_size;
1074 if (rsize < UMA_SMALLEST_UNIT)
1075 rsize = UMA_SMALLEST_UNIT;
1076 if (rsize & keg->uk_align)
1077 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1079 keg->uk_rsize = rsize;
1082 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1083 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1084 shsize = sizeof(struct uma_slab_refcnt);
1086 rsize += UMA_FRITM_SZ; /* Account for linkage */
1087 shsize = sizeof(struct uma_slab);
1090 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1091 KASSERT(keg->uk_ipers != 0, ("zone_small_init: ipers is 0"));
1092 memused = keg->uk_ipers * rsize + shsize;
1093 wastedspace = UMA_SLAB_SIZE - memused;
1096 * We can't do OFFPAGE if we're internal or if we've been
1097 * asked to not go to the VM for buckets. If we do this we
1098 * may end up going to the VM (kmem_map) for slabs which we
1099 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1100 * result of UMA_ZONE_VM, which clearly forbids it.
1102 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1103 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1106 if ((wastedspace >= UMA_MAX_WASTE) &&
1107 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1108 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1109 KASSERT(keg->uk_ipers <= 255,
1110 ("zone_small_init: keg->uk_ipers too high!"));
1112 printf("UMA decided we need offpage slab headers for "
1113 "zone: %s, calculated wastedspace = %d, "
1114 "maximum wasted space allowed = %d, "
1115 "calculated ipers = %d, "
1116 "new wasted space = %d\n", zone->uz_name, wastedspace,
1117 UMA_MAX_WASTE, keg->uk_ipers,
1118 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1120 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1121 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1122 keg->uk_flags |= UMA_ZONE_HASH;
1127 * Finish creating a large (> UMA_SLAB_SIZE) uma zone. Just give in and do
1128 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1132 * zone The zone we should initialize
1138 zone_large_init(uma_zone_t zone)
1145 KASSERT(keg != NULL, ("Keg is null in zone_large_init"));
1146 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1147 ("zone_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY zone"));
1149 pages = keg->uk_size / UMA_SLAB_SIZE;
1151 /* Account for remainder */
1152 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1155 keg->uk_ppera = pages;
1158 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1159 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1160 keg->uk_flags |= UMA_ZONE_HASH;
1162 keg->uk_rsize = keg->uk_size;
1166 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1167 * the keg onto the global keg list.
1169 * Arguments/Returns follow uma_ctor specifications
1170 * udata Actually uma_kctor_args
1173 keg_ctor(void *mem, int size, void *udata, int flags)
1175 struct uma_kctor_args *arg = udata;
1176 uma_keg_t keg = mem;
1180 keg->uk_size = arg->size;
1181 keg->uk_init = arg->uminit;
1182 keg->uk_fini = arg->fini;
1183 keg->uk_align = arg->align;
1186 keg->uk_flags = arg->flags;
1187 keg->uk_allocf = page_alloc;
1188 keg->uk_freef = page_free;
1189 keg->uk_recurse = 0;
1190 keg->uk_slabzone = NULL;
1193 * The master zone is passed to us at keg-creation time.
1198 if (arg->flags & UMA_ZONE_VM)
1199 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1201 if (arg->flags & UMA_ZONE_ZINIT)
1202 keg->uk_init = zero_init;
1205 * The +UMA_FRITM_SZ added to uk_size is to account for the
1206 * linkage that is added to the size in zone_small_init(). If
1207 * we don't account for this here then we may end up in
1208 * zone_small_init() with a calculated 'ipers' of 0.
1210 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1211 if ((keg->uk_size+UMA_FRITMREF_SZ) >
1212 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1213 zone_large_init(zone);
1215 zone_small_init(zone);
1217 if ((keg->uk_size+UMA_FRITM_SZ) >
1218 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1219 zone_large_init(zone);
1221 zone_small_init(zone);
1224 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1225 if (keg->uk_flags & UMA_ZONE_REFCNT)
1226 keg->uk_slabzone = slabrefzone;
1228 keg->uk_slabzone = slabzone;
1232 * If we haven't booted yet we need allocations to go through the
1233 * startup cache until the vm is ready.
1235 if (keg->uk_ppera == 1) {
1236 #ifdef UMA_MD_SMALL_ALLOC
1237 keg->uk_allocf = uma_small_alloc;
1238 keg->uk_freef = uma_small_free;
1241 keg->uk_allocf = startup_alloc;
1245 * Initialize keg's lock (shared among zones) through
1248 zone->uz_lock = &keg->uk_lock;
1249 if (arg->flags & UMA_ZONE_MTXCLASS)
1250 ZONE_LOCK_INIT(zone, 1);
1252 ZONE_LOCK_INIT(zone, 0);
1255 * If we're putting the slab header in the actual page we need to
1256 * figure out where in each page it goes. This calculates a right
1257 * justified offset into the memory on an ALIGN_PTR boundary.
1259 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1262 /* Size of the slab struct and free list */
1263 if (keg->uk_flags & UMA_ZONE_REFCNT)
1264 totsize = sizeof(struct uma_slab_refcnt) +
1265 keg->uk_ipers * UMA_FRITMREF_SZ;
1267 totsize = sizeof(struct uma_slab) +
1268 keg->uk_ipers * UMA_FRITM_SZ;
1270 if (totsize & UMA_ALIGN_PTR)
1271 totsize = (totsize & ~UMA_ALIGN_PTR) +
1272 (UMA_ALIGN_PTR + 1);
1273 keg->uk_pgoff = UMA_SLAB_SIZE - totsize;
1275 if (keg->uk_flags & UMA_ZONE_REFCNT)
1276 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1277 + keg->uk_ipers * UMA_FRITMREF_SZ;
1279 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1280 + keg->uk_ipers * UMA_FRITM_SZ;
1283 * The only way the following is possible is if with our
1284 * UMA_ALIGN_PTR adjustments we are now bigger than
1285 * UMA_SLAB_SIZE. I haven't checked whether this is
1286 * mathematically possible for all cases, so we make
1289 if (totsize > UMA_SLAB_SIZE) {
1290 printf("zone %s ipers %d rsize %d size %d\n",
1291 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1293 panic("UMA slab won't fit.\n");
1297 if (keg->uk_flags & UMA_ZONE_HASH)
1298 hash_alloc(&keg->uk_hash);
1301 printf("%s(%p) size = %d ipers = %d ppera = %d pgoff = %d\n",
1302 zone->uz_name, zone,
1303 keg->uk_size, keg->uk_ipers,
1304 keg->uk_ppera, keg->uk_pgoff);
1307 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1310 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1311 mtx_unlock(&uma_mtx);
1316 * Zone header ctor. This initializes all fields, locks, etc.
1318 * Arguments/Returns follow uma_ctor specifications
1319 * udata Actually uma_zctor_args
1323 zone_ctor(void *mem, int size, void *udata, int flags)
1325 struct uma_zctor_args *arg = udata;
1326 uma_zone_t zone = mem;
1331 zone->uz_name = arg->name;
1332 zone->uz_ctor = arg->ctor;
1333 zone->uz_dtor = arg->dtor;
1334 zone->uz_init = NULL;
1335 zone->uz_fini = NULL;
1336 zone->uz_allocs = 0;
1338 zone->uz_fills = zone->uz_count = 0;
1340 if (arg->flags & UMA_ZONE_SECONDARY) {
1341 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1344 zone->uz_init = arg->uminit;
1345 zone->uz_fini = arg->fini;
1346 zone->uz_lock = &keg->uk_lock;
1349 keg->uk_flags |= UMA_ZONE_SECONDARY;
1350 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1351 if (LIST_NEXT(z, uz_link) == NULL) {
1352 LIST_INSERT_AFTER(z, zone, uz_link);
1357 mtx_unlock(&uma_mtx);
1358 } else if (arg->keg == NULL) {
1359 if (uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1360 arg->align, arg->flags) == NULL)
1363 struct uma_kctor_args karg;
1366 /* We should only be here from uma_startup() */
1367 karg.size = arg->size;
1368 karg.uminit = arg->uminit;
1369 karg.fini = arg->fini;
1370 karg.align = arg->align;
1371 karg.flags = arg->flags;
1373 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1379 zone->uz_lock = &keg->uk_lock;
1382 * Some internal zones don't have room allocated for the per cpu
1383 * caches. If we're internal, bail out here.
1385 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1386 KASSERT((keg->uk_flags & UMA_ZONE_SECONDARY) == 0,
1387 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1391 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1392 zone->uz_count = BUCKET_MAX;
1393 else if (keg->uk_ipers <= BUCKET_MAX)
1394 zone->uz_count = keg->uk_ipers;
1396 zone->uz_count = BUCKET_MAX;
1401 * Keg header dtor. This frees all data, destroys locks, frees the hash
1402 * table and removes the keg from the global list.
1404 * Arguments/Returns follow uma_dtor specifications
1408 keg_dtor(void *arg, int size, void *udata)
1412 keg = (uma_keg_t)arg;
1413 mtx_lock(&keg->uk_lock);
1414 if (keg->uk_free != 0) {
1415 printf("Freed UMA keg was not empty (%d items). "
1416 " Lost %d pages of memory.\n",
1417 keg->uk_free, keg->uk_pages);
1419 mtx_unlock(&keg->uk_lock);
1421 if (keg->uk_flags & UMA_ZONE_HASH)
1422 hash_free(&keg->uk_hash);
1424 mtx_destroy(&keg->uk_lock);
1430 * Arguments/Returns follow uma_dtor specifications
1434 zone_dtor(void *arg, int size, void *udata)
1439 zone = (uma_zone_t)arg;
1442 if (!(keg->uk_flags & UMA_ZFLAG_INTERNAL))
1447 if (keg->uk_flags & UMA_ZONE_SECONDARY) {
1448 LIST_REMOVE(zone, uz_link);
1450 * XXX there are some races here where
1451 * the zone can be drained but zone lock
1452 * released and then refilled before we
1453 * remove it... we dont care for now
1456 if (LIST_EMPTY(&keg->uk_zones))
1457 keg->uk_flags &= ~UMA_ZONE_SECONDARY;
1459 mtx_unlock(&uma_mtx);
1461 LIST_REMOVE(keg, uk_link);
1462 LIST_REMOVE(zone, uz_link);
1463 mtx_unlock(&uma_mtx);
1464 uma_zfree_internal(kegs, keg, NULL, SKIP_NONE);
1466 zone->uz_keg = NULL;
1470 * Traverses every zone in the system and calls a callback
1473 * zfunc A pointer to a function which accepts a zone
1480 zone_foreach(void (*zfunc)(uma_zone_t))
1486 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1487 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1490 mtx_unlock(&uma_mtx);
1493 /* Public functions */
1496 uma_startup(void *bootmem)
1498 struct uma_zctor_args args;
1501 u_int objsize, totsize, wsize;
1505 printf("Creating uma keg headers zone and keg.\n");
1508 * The general UMA lock is a recursion-allowed lock because
1509 * there is a code path where, while we're still configured
1510 * to use startup_alloc() for backend page allocations, we
1511 * may end up in uma_reclaim() which calls zone_foreach(zone_drain),
1512 * which grabs uma_mtx, only to later call into startup_alloc()
1513 * because while freeing we needed to allocate a bucket. Since
1514 * startup_alloc() also takes uma_mtx, we need to be able to
1517 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF | MTX_RECURSE);
1520 * Figure out the maximum number of items-per-slab we'll have if
1521 * we're using the OFFPAGE slab header to track free items, given
1522 * all possible object sizes and the maximum desired wastage
1525 * We iterate until we find an object size for
1526 * which the calculated wastage in zone_small_init() will be
1527 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1528 * is an overall increasing see-saw function, we find the smallest
1529 * objsize such that the wastage is always acceptable for objects
1530 * with that objsize or smaller. Since a smaller objsize always
1531 * generates a larger possible uma_max_ipers, we use this computed
1532 * objsize to calculate the largest ipers possible. Since the
1533 * ipers calculated for OFFPAGE slab headers is always larger than
1534 * the ipers initially calculated in zone_small_init(), we use
1535 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1536 * obtain the maximum ipers possible for offpage slab headers.
1538 * It should be noted that ipers versus objsize is an inversly
1539 * proportional function which drops off rather quickly so as
1540 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1541 * falls into the portion of the inverse relation AFTER the steep
1542 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1544 * Note that we have 8-bits (1 byte) to use as a freelist index
1545 * inside the actual slab header itself and this is enough to
1546 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1547 * object with offpage slab header would have ipers =
1548 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1549 * 1 greater than what our byte-integer freelist index can
1550 * accomodate, but we know that this situation never occurs as
1551 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1552 * that we need to go to offpage slab headers. Or, if we do,
1553 * then we trap that condition below and panic in the INVARIANTS case.
1555 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1557 objsize = UMA_SMALLEST_UNIT;
1558 while (totsize >= wsize) {
1559 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1560 (objsize + UMA_FRITM_SZ);
1561 totsize *= (UMA_FRITM_SZ + objsize);
1564 if (objsize > UMA_SMALLEST_UNIT)
1566 uma_max_ipers = UMA_SLAB_SIZE / objsize;
1568 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1570 objsize = UMA_SMALLEST_UNIT;
1571 while (totsize >= wsize) {
1572 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1573 (objsize + UMA_FRITMREF_SZ);
1574 totsize *= (UMA_FRITMREF_SZ + objsize);
1577 if (objsize > UMA_SMALLEST_UNIT)
1579 uma_max_ipers_ref = UMA_SLAB_SIZE / objsize;
1581 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1582 ("uma_startup: calculated uma_max_ipers values too large!"));
1585 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1586 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1590 /* "manually" create the initial zone */
1591 args.name = "UMA Kegs";
1592 args.size = sizeof(struct uma_keg);
1593 args.ctor = keg_ctor;
1594 args.dtor = keg_dtor;
1595 args.uminit = zero_init;
1597 args.keg = &masterkeg;
1598 args.align = 32 - 1;
1599 args.flags = UMA_ZFLAG_INTERNAL;
1600 /* The initial zone has no Per cpu queues so it's smaller */
1601 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1604 printf("Filling boot free list.\n");
1606 for (i = 0; i < UMA_BOOT_PAGES; i++) {
1607 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1608 slab->us_data = (u_int8_t *)slab;
1609 slab->us_flags = UMA_SLAB_BOOT;
1610 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1615 printf("Creating uma zone headers zone and keg.\n");
1617 args.name = "UMA Zones";
1618 args.size = sizeof(struct uma_zone) +
1619 (sizeof(struct uma_cache) * (mp_maxid + 1));
1620 args.ctor = zone_ctor;
1621 args.dtor = zone_dtor;
1622 args.uminit = zero_init;
1625 args.align = 32 - 1;
1626 args.flags = UMA_ZFLAG_INTERNAL;
1627 /* The initial zone has no Per cpu queues so it's smaller */
1628 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1631 printf("Initializing pcpu cache locks.\n");
1634 printf("Creating slab and hash zones.\n");
1638 * This is the max number of free list items we'll have with
1641 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1642 slabsize += sizeof(struct uma_slab);
1644 /* Now make a zone for slab headers */
1645 slabzone = uma_zcreate("UMA Slabs",
1647 NULL, NULL, NULL, NULL,
1648 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1651 * We also create a zone for the bigger slabs with reference
1652 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1654 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1655 slabsize += sizeof(struct uma_slab_refcnt);
1656 slabrefzone = uma_zcreate("UMA RCntSlabs",
1658 NULL, NULL, NULL, NULL,
1660 UMA_ZFLAG_INTERNAL);
1662 hashzone = uma_zcreate("UMA Hash",
1663 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1664 NULL, NULL, NULL, NULL,
1665 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1669 #ifdef UMA_MD_SMALL_ALLOC
1674 printf("UMA startup complete.\n");
1685 printf("UMA startup2 complete.\n");
1690 * Initialize our callout handle
1698 printf("Starting callout.\n");
1700 callout_init(&uma_callout, CALLOUT_MPSAFE);
1701 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1703 printf("UMA startup3 complete.\n");
1708 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1709 int align, u_int16_t flags)
1711 struct uma_kctor_args args;
1714 args.uminit = uminit;
1719 return (uma_zalloc_internal(kegs, &args, M_WAITOK));
1724 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1725 uma_init uminit, uma_fini fini, int align, u_int16_t flags)
1728 struct uma_zctor_args args;
1730 /* This stuff is essential for the zone ctor */
1735 args.uminit = uminit;
1741 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1746 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1747 uma_init zinit, uma_fini zfini, uma_zone_t master)
1749 struct uma_zctor_args args;
1752 args.size = master->uz_keg->uk_size;
1755 args.uminit = zinit;
1757 args.align = master->uz_keg->uk_align;
1758 args.flags = master->uz_keg->uk_flags | UMA_ZONE_SECONDARY;
1759 args.keg = master->uz_keg;
1761 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1766 uma_zdestroy(uma_zone_t zone)
1768 uma_zfree_internal(zones, zone, NULL, SKIP_NONE);
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,
1857 bzero(item, zone->uz_keg->uk_size);
1859 } else if (cache->uc_freebucket) {
1861 * We have run out of items in our allocbucket.
1862 * See if we can switch with our free bucket.
1864 if (cache->uc_freebucket->ub_cnt > 0) {
1865 #ifdef UMA_DEBUG_ALLOC
1866 printf("uma_zalloc: Swapping empty with"
1869 bucket = cache->uc_freebucket;
1870 cache->uc_freebucket = cache->uc_allocbucket;
1871 cache->uc_allocbucket = bucket;
1878 * Attempt to retrieve the item from the per-CPU cache has failed, so
1879 * we must go back to the zone. This requires the zone lock, so we
1880 * must drop the critical section, then re-acquire it when we go back
1881 * to the cache. Since the critical section is released, we may be
1882 * preempted or migrate. As such, make sure not to maintain any
1883 * thread-local state specific to the cache from prior to releasing
1884 * the critical section.
1890 cache = &zone->uz_cpu[cpu];
1891 bucket = cache->uc_allocbucket;
1892 if (bucket != NULL) {
1893 if (bucket->ub_cnt > 0) {
1897 bucket = cache->uc_freebucket;
1898 if (bucket != NULL && bucket->ub_cnt > 0) {
1904 /* Since we have locked the zone we may as well send back our stats */
1905 zone->uz_allocs += cache->uc_allocs;
1906 cache->uc_allocs = 0;
1907 zone->uz_frees += cache->uc_frees;
1908 cache->uc_frees = 0;
1910 /* Our old one is now a free bucket */
1911 if (cache->uc_allocbucket) {
1912 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
1913 ("uma_zalloc_arg: Freeing a non free bucket."));
1914 LIST_INSERT_HEAD(&zone->uz_free_bucket,
1915 cache->uc_allocbucket, ub_link);
1916 cache->uc_allocbucket = NULL;
1919 /* Check the free list for a new alloc bucket */
1920 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
1921 KASSERT(bucket->ub_cnt != 0,
1922 ("uma_zalloc_arg: Returning an empty bucket."));
1924 LIST_REMOVE(bucket, ub_link);
1925 cache->uc_allocbucket = bucket;
1929 /* We are no longer associated with this CPU. */
1932 /* Bump up our uz_count so we get here less */
1933 if (zone->uz_count < BUCKET_MAX)
1937 * Now lets just fill a bucket and put it on the free list. If that
1938 * works we'll restart the allocation from the begining.
1940 if (uma_zalloc_bucket(zone, flags)) {
1942 goto zalloc_restart;
1946 * We may not be able to get a bucket so return an actual item.
1949 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
1952 return (uma_zalloc_internal(zone, udata, flags));
1956 uma_zone_slab(uma_zone_t zone, int flags)
1964 * This is to prevent us from recursively trying to allocate
1965 * buckets. The problem is that if an allocation forces us to
1966 * grab a new bucket we will call page_alloc, which will go off
1967 * and cause the vm to allocate vm_map_entries. If we need new
1968 * buckets there too we will recurse in kmem_alloc and bad
1969 * things happen. So instead we return a NULL bucket, and make
1970 * the code that allocates buckets smart enough to deal with it
1972 * XXX: While we want this protection for the bucket zones so that
1973 * recursion from the VM is handled (and the calling code that
1974 * allocates buckets knows how to deal with it), we do not want
1975 * to prevent allocation from the slab header zones (slabzone
1976 * and slabrefzone) if uk_recurse is not zero for them. The
1977 * reason is that it could lead to NULL being returned for
1978 * slab header allocations even in the M_WAITOK case, and the
1979 * caller can't handle that.
1981 if (keg->uk_flags & UMA_ZFLAG_INTERNAL && keg->uk_recurse != 0)
1982 if ((zone != slabzone) && (zone != slabrefzone))
1989 * Find a slab with some space. Prefer slabs that are partially
1990 * used over those that are totally full. This helps to reduce
1993 if (keg->uk_free != 0) {
1994 if (!LIST_EMPTY(&keg->uk_part_slab)) {
1995 slab = LIST_FIRST(&keg->uk_part_slab);
1997 slab = LIST_FIRST(&keg->uk_free_slab);
1998 LIST_REMOVE(slab, us_link);
1999 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2006 * M_NOVM means don't ask at all!
2011 if (keg->uk_maxpages &&
2012 keg->uk_pages >= keg->uk_maxpages) {
2013 keg->uk_flags |= UMA_ZFLAG_FULL;
2015 if (flags & M_NOWAIT)
2018 msleep(keg, &keg->uk_lock, PVM,
2023 slab = slab_zalloc(zone, flags);
2027 * If we got a slab here it's safe to mark it partially used
2028 * and return. We assume that the caller is going to remove
2029 * at least one item.
2032 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2036 * We might not have been able to get a slab but another cpu
2037 * could have while we were unlocked. Check again before we
2040 if (flags & M_NOWAIT)
2047 uma_slab_alloc(uma_zone_t zone, uma_slab_t slab)
2050 uma_slabrefcnt_t slabref;
2056 freei = slab->us_firstfree;
2057 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2058 slabref = (uma_slabrefcnt_t)slab;
2059 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2061 slab->us_firstfree = slab->us_freelist[freei].us_item;
2063 item = slab->us_data + (keg->uk_rsize * freei);
2065 slab->us_freecount--;
2068 uma_dbg_alloc(zone, slab, item);
2070 /* Move this slab to the full list */
2071 if (slab->us_freecount == 0) {
2072 LIST_REMOVE(slab, us_link);
2073 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2080 uma_zalloc_bucket(uma_zone_t zone, int flags)
2082 uma_bucket_t bucket;
2085 int max, origflags = flags;
2088 * Try this zone's free list first so we don't allocate extra buckets.
2090 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2091 KASSERT(bucket->ub_cnt == 0,
2092 ("uma_zalloc_bucket: Bucket on free list is not empty."));
2093 LIST_REMOVE(bucket, ub_link);
2097 bflags = (flags & ~M_ZERO);
2098 if (zone->uz_keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2102 bucket = bucket_alloc(zone->uz_count, bflags);
2111 * This code is here to limit the number of simultaneous bucket fills
2112 * for any given zone to the number of per cpu caches in this zone. This
2113 * is done so that we don't allocate more memory than we really need.
2115 if (zone->uz_fills >= mp_ncpus)
2121 max = MIN(bucket->ub_entries, zone->uz_count);
2122 /* Try to keep the buckets totally full */
2123 saved = bucket->ub_cnt;
2124 while (bucket->ub_cnt < max &&
2125 (slab = uma_zone_slab(zone, flags)) != NULL) {
2126 while (slab->us_freecount && bucket->ub_cnt < max) {
2127 bucket->ub_bucket[bucket->ub_cnt++] =
2128 uma_slab_alloc(zone, slab);
2131 /* Don't block on the next fill */
2136 * We unlock here because we need to call the zone's init.
2137 * It should be safe to unlock because the slab dealt with
2138 * above is already on the appropriate list within the keg
2139 * and the bucket we filled is not yet on any list, so we
2142 if (zone->uz_init != NULL) {
2146 for (i = saved; i < bucket->ub_cnt; i++)
2147 if (zone->uz_init(bucket->ub_bucket[i],
2148 zone->uz_keg->uk_size, origflags) != 0)
2151 * If we couldn't initialize the whole bucket, put the
2152 * rest back onto the freelist.
2154 if (i != bucket->ub_cnt) {
2157 for (j = i; j < bucket->ub_cnt; j++) {
2158 uma_zfree_internal(zone, bucket->ub_bucket[j],
2161 bucket->ub_bucket[j] = NULL;
2170 if (bucket->ub_cnt != 0) {
2171 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2178 bucket_free(bucket);
2183 * Allocates an item for an internal zone
2186 * zone The zone to alloc for.
2187 * udata The data to be passed to the constructor.
2188 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2191 * NULL if there is no memory and M_NOWAIT is set
2192 * An item if successful
2196 uma_zalloc_internal(uma_zone_t zone, void *udata, int flags)
2205 #ifdef UMA_DEBUG_ALLOC
2206 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2210 slab = uma_zone_slab(zone, flags);
2216 item = uma_slab_alloc(zone, slab);
2223 * We have to call both the zone's init (not the keg's init)
2224 * and the zone's ctor. This is because the item is going from
2225 * a keg slab directly to the user, and the user is expecting it
2226 * to be both zone-init'd as well as zone-ctor'd.
2228 if (zone->uz_init != NULL) {
2229 if (zone->uz_init(item, keg->uk_size, flags) != 0) {
2230 uma_zfree_internal(zone, item, udata, SKIP_FINI);
2234 if (zone->uz_ctor != NULL) {
2235 if (zone->uz_ctor(item, keg->uk_size, udata, flags) != 0) {
2236 uma_zfree_internal(zone, item, udata, SKIP_DTOR);
2241 bzero(item, keg->uk_size);
2248 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2252 uma_bucket_t bucket;
2258 #ifdef UMA_DEBUG_ALLOC_1
2259 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2261 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2265 zone->uz_dtor(item, keg->uk_size, udata);
2268 if (keg->uk_flags & UMA_ZONE_MALLOC)
2269 uma_dbg_free(zone, udata, item);
2271 uma_dbg_free(zone, NULL, item);
2275 * The race here is acceptable. If we miss it we'll just have to wait
2276 * a little longer for the limits to be reset.
2278 if (keg->uk_flags & UMA_ZFLAG_FULL)
2279 goto zfree_internal;
2282 * If possible, free to the per-CPU cache. There are two
2283 * requirements for safe access to the per-CPU cache: (1) the thread
2284 * accessing the cache must not be preempted or yield during access,
2285 * and (2) the thread must not migrate CPUs without switching which
2286 * cache it accesses. We rely on a critical section to prevent
2287 * preemption and migration. We release the critical section in
2288 * order to acquire the zone mutex if we are unable to free to the
2289 * current cache; when we re-acquire the critical section, we must
2290 * detect and handle migration if it has occurred.
2295 cache = &zone->uz_cpu[cpu];
2298 bucket = cache->uc_freebucket;
2302 * Do we have room in our bucket? It is OK for this uz count
2303 * check to be slightly out of sync.
2306 if (bucket->ub_cnt < bucket->ub_entries) {
2307 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2308 ("uma_zfree: Freeing to non free bucket index."));
2309 bucket->ub_bucket[bucket->ub_cnt] = item;
2314 } else if (cache->uc_allocbucket) {
2315 #ifdef UMA_DEBUG_ALLOC
2316 printf("uma_zfree: Swapping buckets.\n");
2319 * We have run out of space in our freebucket.
2320 * See if we can switch with our alloc bucket.
2322 if (cache->uc_allocbucket->ub_cnt <
2323 cache->uc_freebucket->ub_cnt) {
2324 bucket = cache->uc_freebucket;
2325 cache->uc_freebucket = cache->uc_allocbucket;
2326 cache->uc_allocbucket = bucket;
2332 * We can get here for two reasons:
2334 * 1) The buckets are NULL
2335 * 2) The alloc and free buckets are both somewhat full.
2337 * We must go back the zone, which requires acquiring the zone lock,
2338 * which in turn means we must release and re-acquire the critical
2339 * section. Since the critical section is released, we may be
2340 * preempted or migrate. As such, make sure not to maintain any
2341 * thread-local state specific to the cache from prior to releasing
2342 * the critical section.
2348 cache = &zone->uz_cpu[cpu];
2349 if (cache->uc_freebucket != NULL) {
2350 if (cache->uc_freebucket->ub_cnt <
2351 cache->uc_freebucket->ub_entries) {
2355 if (cache->uc_allocbucket != NULL &&
2356 (cache->uc_allocbucket->ub_cnt <
2357 cache->uc_freebucket->ub_cnt)) {
2363 bucket = cache->uc_freebucket;
2364 cache->uc_freebucket = NULL;
2366 /* Can we throw this on the zone full list? */
2367 if (bucket != NULL) {
2368 #ifdef UMA_DEBUG_ALLOC
2369 printf("uma_zfree: Putting old bucket on the free list.\n");
2371 /* ub_cnt is pointing to the last free item */
2372 KASSERT(bucket->ub_cnt != 0,
2373 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2374 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2377 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2378 LIST_REMOVE(bucket, ub_link);
2380 cache->uc_freebucket = bucket;
2383 /* We are no longer associated with this CPU. */
2386 /* And the zone.. */
2389 #ifdef UMA_DEBUG_ALLOC
2390 printf("uma_zfree: Allocating new free bucket.\n");
2394 if (keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2396 bucket = bucket_alloc(zone->uz_count, bflags);
2399 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2406 * If nothing else caught this, we'll just do an internal free.
2409 uma_zfree_internal(zone, item, udata, SKIP_DTOR);
2415 * Frees an item to an INTERNAL zone or allocates a free bucket
2418 * zone The zone to free to
2419 * item The item we're freeing
2420 * udata User supplied data for the dtor
2421 * skip Skip dtors and finis
2424 uma_zfree_internal(uma_zone_t zone, void *item, void *udata,
2425 enum zfreeskip skip)
2428 uma_slabrefcnt_t slabref;
2435 if (skip < SKIP_DTOR && zone->uz_dtor)
2436 zone->uz_dtor(item, keg->uk_size, udata);
2437 if (skip < SKIP_FINI && zone->uz_fini)
2438 zone->uz_fini(item, keg->uk_size);
2442 if (!(keg->uk_flags & UMA_ZONE_MALLOC)) {
2443 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2444 if (keg->uk_flags & UMA_ZONE_HASH)
2445 slab = hash_sfind(&keg->uk_hash, mem);
2447 mem += keg->uk_pgoff;
2448 slab = (uma_slab_t)mem;
2451 slab = (uma_slab_t)udata;
2454 /* Do we need to remove from any lists? */
2455 if (slab->us_freecount+1 == keg->uk_ipers) {
2456 LIST_REMOVE(slab, us_link);
2457 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2458 } else if (slab->us_freecount == 0) {
2459 LIST_REMOVE(slab, us_link);
2460 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2463 /* Slab management stuff */
2464 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2469 uma_dbg_free(zone, slab, item);
2472 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2473 slabref = (uma_slabrefcnt_t)slab;
2474 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2476 slab->us_freelist[freei].us_item = slab->us_firstfree;
2478 slab->us_firstfree = freei;
2479 slab->us_freecount++;
2481 /* 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;
2489 /* We can handle one more allocation */
2498 uma_zone_set_max(uma_zone_t zone, int nitems)
2504 if (keg->uk_ppera > 1)
2505 keg->uk_maxpages = nitems * keg->uk_ppera;
2507 keg->uk_maxpages = nitems / keg->uk_ipers;
2509 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2517 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2520 KASSERT(zone->uz_keg->uk_pages == 0,
2521 ("uma_zone_set_init on non-empty keg"));
2522 zone->uz_keg->uk_init = uminit;
2528 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2531 KASSERT(zone->uz_keg->uk_pages == 0,
2532 ("uma_zone_set_fini on non-empty keg"));
2533 zone->uz_keg->uk_fini = fini;
2539 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2542 KASSERT(zone->uz_keg->uk_pages == 0,
2543 ("uma_zone_set_zinit on non-empty keg"));
2544 zone->uz_init = zinit;
2550 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2553 KASSERT(zone->uz_keg->uk_pages == 0,
2554 ("uma_zone_set_zfini on non-empty keg"));
2555 zone->uz_fini = zfini;
2560 /* XXX uk_freef is not actually used with the zone locked */
2562 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2565 zone->uz_keg->uk_freef = freef;
2570 /* XXX uk_allocf is not actually used with the zone locked */
2572 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2575 zone->uz_keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2576 zone->uz_keg->uk_allocf = allocf;
2582 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2589 pages = count / keg->uk_ipers;
2591 if (pages * keg->uk_ipers < count)
2594 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2599 obj = vm_object_allocate(OBJT_DEFAULT,
2602 VM_OBJECT_LOCK_INIT(obj, "uma object");
2603 _vm_object_allocate(OBJT_DEFAULT,
2609 keg->uk_maxpages = pages;
2610 keg->uk_allocf = obj_alloc;
2611 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2618 uma_prealloc(uma_zone_t zone, int items)
2626 slabs = items / keg->uk_ipers;
2627 if (slabs * keg->uk_ipers < items)
2630 slab = slab_zalloc(zone, M_WAITOK);
2631 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2639 uma_find_refcnt(uma_zone_t zone, void *item)
2641 uma_slabrefcnt_t slabref;
2647 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
2649 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
2650 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
2651 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
2653 refcnt = &slabref->us_freelist[idx].us_refcnt;
2662 printf("UMA: vm asked us to release pages!\n");
2665 zone_foreach(zone_drain);
2667 * Some slabs may have been freed but this zone will be visited early
2668 * we visit again so that we can free pages that are empty once other
2669 * zones are drained. We have to do the same for buckets.
2671 zone_drain(slabzone);
2672 zone_drain(slabrefzone);
2673 bucket_zone_drain();
2677 uma_large_malloc(int size, int wait)
2683 slab = uma_zalloc_internal(slabzone, NULL, wait);
2686 mem = page_alloc(NULL, size, &flags, wait);
2688 vsetslab((vm_offset_t)mem, slab);
2689 slab->us_data = mem;
2690 slab->us_flags = flags | UMA_SLAB_MALLOC;
2691 slab->us_size = size;
2693 uma_zfree_internal(slabzone, slab, NULL, SKIP_NONE);
2700 uma_large_free(uma_slab_t slab)
2702 vsetobj((vm_offset_t)slab->us_data, kmem_object);
2703 page_free(slab->us_data, slab->us_size, slab->us_flags);
2704 uma_zfree_internal(slabzone, slab, NULL, SKIP_NONE);
2708 uma_print_stats(void)
2710 zone_foreach(uma_print_zone);
2714 slab_print(uma_slab_t slab)
2716 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
2717 slab->us_keg, slab->us_data, slab->us_freecount,
2718 slab->us_firstfree);
2722 cache_print(uma_cache_t cache)
2724 printf("alloc: %p(%d), free: %p(%d)\n",
2725 cache->uc_allocbucket,
2726 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
2727 cache->uc_freebucket,
2728 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
2732 uma_print_zone(uma_zone_t zone)
2740 printf("%s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
2741 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
2742 keg->uk_ipers, keg->uk_ppera,
2743 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
2744 printf("Part slabs:\n");
2745 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
2747 printf("Free slabs:\n");
2748 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
2750 printf("Full slabs:\n");
2751 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
2753 for (i = 0; i <= mp_maxid; i++) {
2756 cache = &zone->uz_cpu[i];
2757 printf("CPU %d Cache:\n", i);
2763 * Generate statistics across both the zone and its per-cpu cache's. Return
2764 * desired statistics if the pointer is non-NULL for that statistic.
2766 * Note: does not update the zone statistics, as it can't safely clear the
2767 * per-CPU cache statistic.
2769 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
2770 * safe from off-CPU; we should modify the caches to track this information
2771 * directly so that we don't have to.
2774 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
2778 u_int64_t allocs, frees;
2783 for (cpu = 0; cpu <= mp_maxid; cpu++) {
2784 if (CPU_ABSENT(cpu))
2786 cache = &z->uz_cpu[cpu];
2787 if (cache->uc_allocbucket != NULL)
2788 cachefree += cache->uc_allocbucket->ub_cnt;
2789 if (cache->uc_freebucket != NULL)
2790 cachefree += cache->uc_freebucket->ub_cnt;
2791 allocs += cache->uc_allocs;
2792 frees += cache->uc_frees;
2794 allocs += z->uz_allocs;
2795 frees += z->uz_frees;
2796 if (cachefreep != NULL)
2797 *cachefreep = cachefree;
2798 if (allocsp != NULL)
2805 * Sysctl handler for vm.zone
2807 * stolen from vm_zone.c
2810 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
2812 int error, len, cnt;
2813 const int linesize = 128; /* conservative */
2815 char *tmpbuf, *offset;
2820 uma_bucket_t bucket;
2821 u_int64_t allocs, frees;
2825 LIST_FOREACH(zk, &uma_kegs, uk_link) {
2826 LIST_FOREACH(z, &zk->uk_zones, uz_link)
2829 mtx_unlock(&uma_mtx);
2830 MALLOC(tmpbuf, char *, (cnt == 0 ? 1 : cnt) * linesize,
2832 len = snprintf(tmpbuf, linesize,
2833 "\nITEM SIZE LIMIT USED FREE REQUESTS\n\n");
2835 tmpbuf[len - 1] = '\0';
2836 error = SYSCTL_OUT(req, tmpbuf, cnt == 0 ? len-1 : len);
2837 if (error || cnt == 0)
2841 LIST_FOREACH(zk, &uma_kegs, uk_link) {
2842 LIST_FOREACH(z, &zk->uk_zones, uz_link) {
2843 if (cnt == 0) /* list may have changed size */
2847 if (!(zk->uk_flags & UMA_ZFLAG_INTERNAL)) {
2848 uma_zone_sumstat(z, &cachefree, &allocs, &frees);
2850 allocs = z->uz_allocs;
2851 frees = z->uz_frees;
2854 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link) {
2855 cachefree += bucket->ub_cnt;
2857 totalfree = zk->uk_free + cachefree;
2858 len = snprintf(offset, linesize,
2859 "%-12.12s %6.6u, %8.8u, %6.6u, %6.6u, %8.8llu\n",
2860 z->uz_name, zk->uk_size,
2861 zk->uk_maxpages * zk->uk_ipers,
2862 (zk->uk_ipers * (zk->uk_pages / zk->uk_ppera)) - totalfree,
2864 (unsigned long long)allocs);
2866 for (p = offset + 12; p > offset && *p == ' '; --p)
2873 mtx_unlock(&uma_mtx);
2875 error = SYSCTL_OUT(req, tmpbuf, offset - tmpbuf);
2877 FREE(tmpbuf, M_TEMP);
2882 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
2890 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2891 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2894 mtx_unlock(&uma_mtx);
2895 return (sysctl_handle_int(oidp, &count, 0, req));
2899 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
2901 struct uma_stream_header ush;
2902 struct uma_type_header uth;
2903 struct uma_percpu_stat ups;
2904 uma_bucket_t bucket;
2910 int buflen, count, error, i;
2914 mtx_assert(&uma_mtx, MA_OWNED);
2916 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2917 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2920 mtx_unlock(&uma_mtx);
2922 buflen = sizeof(ush) + count * (sizeof(uth) + sizeof(ups) *
2924 buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
2928 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2929 LIST_FOREACH(z, &kz->uk_zones, uz_link)
2933 free(buffer, M_TEMP);
2938 sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
2941 * Insert stream header.
2943 bzero(&ush, sizeof(ush));
2944 ush.ush_version = UMA_STREAM_VERSION;
2945 ush.ush_maxcpus = MAXCPU;
2946 ush.ush_count = count;
2947 if (sbuf_bcat(&sbuf, &ush, sizeof(ush)) < 0) {
2948 mtx_unlock(&uma_mtx);
2953 LIST_FOREACH(kz, &uma_kegs, uk_link) {
2954 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
2955 bzero(&uth, sizeof(uth));
2957 strlcpy(uth.uth_name, z->uz_name, UMA_MAX_NAME);
2958 uth.uth_align = kz->uk_align;
2959 uth.uth_pages = kz->uk_pages;
2960 uth.uth_keg_free = kz->uk_free;
2961 uth.uth_size = kz->uk_size;
2962 uth.uth_rsize = kz->uk_rsize;
2963 uth.uth_maxpages = kz->uk_maxpages;
2964 if (kz->uk_ppera > 1)
2965 uth.uth_limit = kz->uk_maxpages /
2968 uth.uth_limit = kz->uk_maxpages *
2970 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
2971 uth.uth_zone_free += bucket->ub_cnt;
2972 uth.uth_allocs = z->uz_allocs;
2973 uth.uth_frees = z->uz_frees;
2975 if (sbuf_bcat(&sbuf, &uth, sizeof(uth)) < 0) {
2976 mtx_unlock(&uma_mtx);
2981 * XXXRW: Should not access bucket fields from
2982 * non-local CPU. Instead need to modify the caches
2983 * to directly maintain these statistics so we don't
2986 for (i = 0; i < MAXCPU; i++) {
2987 bzero(&ups, sizeof(ups));
2988 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
2990 cache = &z->uz_cpu[i];
2991 if (cache->uc_allocbucket != NULL)
2992 ups.ups_cache_free +=
2993 cache->uc_allocbucket->ub_cnt;
2994 if (cache->uc_freebucket != NULL)
2995 ups.ups_cache_free +=
2996 cache->uc_freebucket->ub_cnt;
2997 ups.ups_allocs = cache->uc_allocs;
2998 ups.ups_frees = cache->uc_frees;
3000 if (sbuf_bcat(&sbuf, &ups, sizeof(ups)) < 0) {
3001 mtx_unlock(&uma_mtx);
3008 mtx_unlock(&uma_mtx);
3010 error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
3012 free(buffer, M_TEMP);