2 * Copyright (c) 2002-2005, 2009 Jeffrey Roberson <jeff@FreeBSD.org>
3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4 * Copyright (c) 2004-2006 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * uma_core.c Implementation of the Universal Memory allocator
32 * This allocator is intended to replace the multitude of similar object caches
33 * in the standard FreeBSD kernel. The intent is to be flexible as well as
34 * effecient. A primary design goal is to return unused memory to the rest of
35 * the system. This will make the system as a whole more flexible due to the
36 * ability to move memory to subsystems which most need it instead of leaving
37 * pools of reserved memory unused.
39 * The basic ideas stem from similar slab/zone based allocators whose algorithms
46 * - Improve memory usage for large allocations
47 * - Investigate cache size adjustments
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 /* I should really use ktr.. */
56 #define UMA_DEBUG_ALLOC 1
57 #define UMA_DEBUG_ALLOC_1 1
61 #include "opt_param.h"
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/types.h>
67 #include <sys/queue.h>
68 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
72 #include <sys/mutex.h>
76 #include <sys/vmmeter.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_param.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_kern.h>
84 #include <vm/vm_extern.h>
86 #include <vm/uma_int.h>
87 #include <vm/uma_dbg.h>
92 * This is the zone and keg from which all zones are spawned. The idea is that
93 * even the zone & keg heads are allocated from the allocator, so we use the
94 * bss section to bootstrap us.
96 static struct uma_keg masterkeg;
97 static struct uma_zone masterzone_k;
98 static struct uma_zone masterzone_z;
99 static uma_zone_t kegs = &masterzone_k;
100 static uma_zone_t zones = &masterzone_z;
102 /* This is the zone from which all of uma_slab_t's are allocated. */
103 static uma_zone_t slabzone;
104 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
107 * The initial hash tables come out of this zone so they can be allocated
108 * prior to malloc coming up.
110 static uma_zone_t hashzone;
112 /* The boot-time adjusted value for cache line alignment. */
113 int uma_align_cache = 64 - 1;
115 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
118 * Are we allowed to allocate buckets?
120 static int bucketdisable = 1;
122 /* Linked list of all kegs in the system */
123 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
125 /* This mutex protects the keg list */
126 static struct mtx uma_mtx;
128 /* Linked list of boot time pages */
129 static LIST_HEAD(,uma_slab) uma_boot_pages =
130 LIST_HEAD_INITIALIZER(uma_boot_pages);
132 /* This mutex protects the boot time pages list */
133 static struct mtx uma_boot_pages_mtx;
135 /* Is the VM done starting up? */
136 static int booted = 0;
137 #define UMA_STARTUP 1
138 #define UMA_STARTUP2 2
140 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
141 static u_int uma_max_ipers;
142 static u_int uma_max_ipers_ref;
145 * This is the handle used to schedule events that need to happen
146 * outside of the allocation fast path.
148 static struct callout uma_callout;
149 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
152 * This structure is passed as the zone ctor arg so that I don't have to create
153 * a special allocation function just for zones.
155 struct uma_zctor_args {
167 struct uma_kctor_args {
176 struct uma_bucket_zone {
182 #define BUCKET_MAX 128
184 struct uma_bucket_zone bucket_zones[] = {
185 { NULL, "16 Bucket", 16 },
186 { NULL, "32 Bucket", 32 },
187 { NULL, "64 Bucket", 64 },
188 { NULL, "128 Bucket", 128 },
192 #define BUCKET_SHIFT 4
193 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
196 * bucket_size[] maps requested bucket sizes to zones that allocate a bucket
197 * of approximately the right size.
199 static uint8_t bucket_size[BUCKET_ZONES];
202 * Flags and enumerations to be passed to internal functions.
204 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
206 #define ZFREE_STATFAIL 0x00000001 /* Update zone failure statistic. */
207 #define ZFREE_STATFREE 0x00000002 /* Update zone free statistic. */
211 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
212 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
213 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
214 static void page_free(void *, int, u_int8_t);
215 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int);
216 static void cache_drain(uma_zone_t);
217 static void bucket_drain(uma_zone_t, uma_bucket_t);
218 static void bucket_cache_drain(uma_zone_t zone);
219 static int keg_ctor(void *, int, void *, int);
220 static void keg_dtor(void *, int, void *);
221 static int zone_ctor(void *, int, void *, int);
222 static void zone_dtor(void *, int, void *);
223 static int zero_init(void *, int, int);
224 static void keg_small_init(uma_keg_t keg);
225 static void keg_large_init(uma_keg_t keg);
226 static void zone_foreach(void (*zfunc)(uma_zone_t));
227 static void zone_timeout(uma_zone_t zone);
228 static int hash_alloc(struct uma_hash *);
229 static int hash_expand(struct uma_hash *, struct uma_hash *);
230 static void hash_free(struct uma_hash *hash);
231 static void uma_timeout(void *);
232 static void uma_startup3(void);
233 static void *zone_alloc_item(uma_zone_t, void *, int);
234 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip,
236 static void bucket_enable(void);
237 static void bucket_init(void);
238 static uma_bucket_t bucket_alloc(int, int);
239 static void bucket_free(uma_bucket_t);
240 static void bucket_zone_drain(void);
241 static int zone_alloc_bucket(uma_zone_t zone, int flags);
242 static uma_slab_t zone_fetch_slab(uma_zone_t zone, uma_keg_t last, int flags);
243 static uma_slab_t zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int flags);
244 static void *slab_alloc_item(uma_zone_t zone, uma_slab_t slab);
245 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
246 uma_fini fini, int align, u_int32_t flags);
247 static inline void zone_relock(uma_zone_t zone, uma_keg_t keg);
248 static inline void keg_relock(uma_keg_t keg, uma_zone_t zone);
250 void uma_print_zone(uma_zone_t);
251 void uma_print_stats(void);
252 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
253 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
255 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
257 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
258 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
260 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
261 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
264 * This routine checks to see whether or not it's safe to enable buckets.
270 bucketdisable = vm_page_count_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,
295 UMA_ZFLAG_INTERNAL | UMA_ZFLAG_BUCKET);
296 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
297 bucket_size[i >> BUCKET_SHIFT] = j;
302 * Given a desired number of entries for a bucket, return the zone from which
303 * to allocate the bucket.
305 static struct uma_bucket_zone *
306 bucket_zone_lookup(int entries)
310 idx = howmany(entries, 1 << BUCKET_SHIFT);
311 return (&bucket_zones[bucket_size[idx]]);
315 bucket_alloc(int entries, int bflags)
317 struct uma_bucket_zone *ubz;
321 * This is to stop us from allocating per cpu buckets while we're
322 * running out of vm.boot_pages. Otherwise, we would exhaust the
323 * boot pages. This also prevents us from allocating buckets in
324 * low memory situations.
329 ubz = bucket_zone_lookup(entries);
330 bucket = zone_alloc_item(ubz->ubz_zone, NULL, bflags);
333 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
336 bucket->ub_entries = ubz->ubz_entries;
343 bucket_free(uma_bucket_t bucket)
345 struct uma_bucket_zone *ubz;
347 ubz = bucket_zone_lookup(bucket->ub_entries);
348 zone_free_item(ubz->ubz_zone, bucket, NULL, SKIP_NONE,
353 bucket_zone_drain(void)
355 struct uma_bucket_zone *ubz;
357 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
358 zone_drain(ubz->ubz_zone);
361 static inline uma_keg_t
362 zone_first_keg(uma_zone_t zone)
365 return (LIST_FIRST(&zone->uz_kegs)->kl_keg);
369 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
373 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
374 kegfn(klink->kl_keg);
378 * Routine called by timeout which is used to fire off some time interval
379 * based calculations. (stats, hash size, etc.)
388 uma_timeout(void *unused)
391 zone_foreach(zone_timeout);
393 /* Reschedule this event */
394 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
398 * Routine to perform timeout driven calculations. This expands the
399 * hashes and does per cpu statistics aggregation.
404 keg_timeout(uma_keg_t keg)
409 * Expand the keg hash table.
411 * This is done if the number of slabs is larger than the hash size.
412 * What I'm trying to do here is completely reduce collisions. This
413 * may be a little aggressive. Should I allow for two collisions max?
415 if (keg->uk_flags & UMA_ZONE_HASH &&
416 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
417 struct uma_hash newhash;
418 struct uma_hash oldhash;
422 * This is so involved because allocating and freeing
423 * while the keg lock is held will lead to deadlock.
424 * I have to do everything in stages and check for
427 newhash = keg->uk_hash;
429 ret = hash_alloc(&newhash);
432 if (hash_expand(&keg->uk_hash, &newhash)) {
433 oldhash = keg->uk_hash;
434 keg->uk_hash = newhash;
447 zone_timeout(uma_zone_t zone)
450 zone_foreach_keg(zone, &keg_timeout);
454 * Allocate and zero fill the next sized hash table from the appropriate
458 * hash A new hash structure with the old hash size in uh_hashsize
461 * 1 on sucess and 0 on failure.
464 hash_alloc(struct uma_hash *hash)
469 oldsize = hash->uh_hashsize;
471 /* We're just going to go to a power of two greater */
473 hash->uh_hashsize = oldsize * 2;
474 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
475 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
476 M_UMAHASH, M_NOWAIT);
478 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
479 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
481 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
483 if (hash->uh_slab_hash) {
484 bzero(hash->uh_slab_hash, alloc);
485 hash->uh_hashmask = hash->uh_hashsize - 1;
493 * Expands the hash table for HASH zones. This is done from zone_timeout
494 * to reduce collisions. This must not be done in the regular allocation
495 * path, otherwise, we can recurse on the vm while allocating pages.
498 * oldhash The hash you want to expand
499 * newhash The hash structure for the new table
507 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
513 if (!newhash->uh_slab_hash)
516 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
520 * I need to investigate hash algorithms for resizing without a
524 for (i = 0; i < oldhash->uh_hashsize; i++)
525 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
526 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
527 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
528 hval = UMA_HASH(newhash, slab->us_data);
529 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
537 * Free the hash bucket to the appropriate backing store.
540 * slab_hash The hash bucket we're freeing
541 * hashsize The number of entries in that hash bucket
547 hash_free(struct uma_hash *hash)
549 if (hash->uh_slab_hash == NULL)
551 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
552 zone_free_item(hashzone,
553 hash->uh_slab_hash, NULL, SKIP_NONE, ZFREE_STATFREE);
555 free(hash->uh_slab_hash, M_UMAHASH);
559 * Frees all outstanding items in a bucket
562 * zone The zone to free to, must be unlocked.
563 * bucket The free/alloc bucket with items, cpu queue must be locked.
570 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
577 while (bucket->ub_cnt > 0) {
579 item = bucket->ub_bucket[bucket->ub_cnt];
581 bucket->ub_bucket[bucket->ub_cnt] = NULL;
582 KASSERT(item != NULL,
583 ("bucket_drain: botched ptr, item is NULL"));
585 zone_free_item(zone, item, NULL, SKIP_DTOR, 0);
590 * Drains the per cpu caches for a zone.
592 * NOTE: This may only be called while the zone is being turn down, and not
593 * during normal operation. This is necessary in order that we do not have
594 * to migrate CPUs to drain the per-CPU caches.
597 * zone The zone to drain, must be unlocked.
603 cache_drain(uma_zone_t zone)
609 * XXX: It is safe to not lock the per-CPU caches, because we're
610 * tearing down the zone anyway. I.e., there will be no further use
611 * of the caches at this point.
613 * XXX: It would good to be able to assert that the zone is being
614 * torn down to prevent improper use of cache_drain().
616 * XXX: We lock the zone before passing into bucket_cache_drain() as
617 * it is used elsewhere. Should the tear-down path be made special
618 * there in some form?
621 cache = &zone->uz_cpu[cpu];
622 bucket_drain(zone, cache->uc_allocbucket);
623 bucket_drain(zone, cache->uc_freebucket);
624 if (cache->uc_allocbucket != NULL)
625 bucket_free(cache->uc_allocbucket);
626 if (cache->uc_freebucket != NULL)
627 bucket_free(cache->uc_freebucket);
628 cache->uc_allocbucket = cache->uc_freebucket = NULL;
631 bucket_cache_drain(zone);
636 * Drain the cached buckets from a zone. Expects a locked zone on entry.
639 bucket_cache_drain(uma_zone_t zone)
644 * Drain the bucket queues and free the buckets, we just keep two per
647 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
648 LIST_REMOVE(bucket, ub_link);
650 bucket_drain(zone, bucket);
655 /* Now we do the free queue.. */
656 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
657 LIST_REMOVE(bucket, ub_link);
663 * Frees pages from a keg back to the system. This is done on demand from
664 * the pageout daemon.
669 keg_drain(uma_keg_t keg)
671 struct slabhead freeslabs = { 0 };
679 * We don't want to take pages from statically allocated kegs at this
682 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
686 printf("%s free items: %u\n", keg->uk_name, keg->uk_free);
689 if (keg->uk_free == 0)
692 slab = LIST_FIRST(&keg->uk_free_slab);
694 n = LIST_NEXT(slab, us_link);
696 /* We have no where to free these to */
697 if (slab->us_flags & UMA_SLAB_BOOT) {
702 LIST_REMOVE(slab, us_link);
703 keg->uk_pages -= keg->uk_ppera;
704 keg->uk_free -= keg->uk_ipers;
706 if (keg->uk_flags & UMA_ZONE_HASH)
707 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
709 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
716 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
717 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
719 for (i = 0; i < keg->uk_ipers; i++)
721 slab->us_data + (keg->uk_rsize * i),
723 flags = slab->us_flags;
726 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
729 if (flags & UMA_SLAB_KMEM)
731 else if (flags & UMA_SLAB_KERNEL)
735 for (i = 0; i < keg->uk_ppera; i++)
736 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
739 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
740 zone_free_item(keg->uk_slabzone, slab, NULL,
741 SKIP_NONE, ZFREE_STATFREE);
743 printf("%s: Returning %d bytes.\n",
744 keg->uk_name, UMA_SLAB_SIZE * keg->uk_ppera);
746 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
751 zone_drain_wait(uma_zone_t zone, int waitok)
755 * Set draining to interlock with zone_dtor() so we can release our
756 * locks as we go. Only dtor() should do a WAITOK call since it
757 * is the only call that knows the structure will still be available
761 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
762 if (waitok == M_NOWAIT)
764 mtx_unlock(&uma_mtx);
765 msleep(zone, zone->uz_lock, PVM, "zonedrain", 1);
768 zone->uz_flags |= UMA_ZFLAG_DRAINING;
769 bucket_cache_drain(zone);
772 * The DRAINING flag protects us from being freed while
773 * we're running. Normally the uma_mtx would protect us but we
774 * must be able to release and acquire the right lock for each keg.
776 zone_foreach_keg(zone, &keg_drain);
778 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
785 zone_drain(uma_zone_t zone)
788 zone_drain_wait(zone, M_NOWAIT);
792 * Allocate a new slab for a keg. This does not insert the slab onto a list.
795 * wait Shall we wait?
798 * The slab that was allocated or NULL if there is no memory and the
799 * caller specified M_NOWAIT.
802 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int wait)
804 uma_slabrefcnt_t slabref;
811 mtx_assert(&keg->uk_lock, MA_OWNED);
815 printf("slab_zalloc: Allocating a new slab for %s\n", keg->uk_name);
817 allocf = keg->uk_allocf;
820 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
821 slab = zone_alloc_item(keg->uk_slabzone, NULL, wait);
829 * This reproduces the old vm_zone behavior of zero filling pages the
830 * first time they are added to a zone.
832 * Malloced items are zeroed in uma_zalloc.
835 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
840 if (keg->uk_flags & UMA_ZONE_NODUMP)
843 /* zone is passed for legacy reasons. */
844 mem = allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE, &flags, wait);
846 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
847 zone_free_item(keg->uk_slabzone, slab, NULL,
848 SKIP_NONE, ZFREE_STATFREE);
853 /* Point the slab into the allocated memory */
854 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
855 slab = (uma_slab_t )(mem + keg->uk_pgoff);
857 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
858 for (i = 0; i < keg->uk_ppera; i++)
859 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
863 slab->us_freecount = keg->uk_ipers;
864 slab->us_firstfree = 0;
865 slab->us_flags = flags;
867 if (keg->uk_flags & UMA_ZONE_REFCNT) {
868 slabref = (uma_slabrefcnt_t)slab;
869 for (i = 0; i < keg->uk_ipers; i++) {
870 slabref->us_freelist[i].us_refcnt = 0;
871 slabref->us_freelist[i].us_item = i+1;
874 for (i = 0; i < keg->uk_ipers; i++)
875 slab->us_freelist[i].us_item = i+1;
878 if (keg->uk_init != NULL) {
879 for (i = 0; i < keg->uk_ipers; i++)
880 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
881 keg->uk_size, wait) != 0)
883 if (i != keg->uk_ipers) {
884 if (keg->uk_fini != NULL) {
885 for (i--; i > -1; i--)
886 keg->uk_fini(slab->us_data +
890 if (keg->uk_flags & UMA_ZONE_VTOSLAB) {
893 if (flags & UMA_SLAB_KMEM)
895 else if (flags & UMA_SLAB_KERNEL)
899 for (i = 0; i < keg->uk_ppera; i++)
900 vsetobj((vm_offset_t)mem +
901 (i * PAGE_SIZE), obj);
903 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
904 zone_free_item(keg->uk_slabzone, slab,
905 NULL, SKIP_NONE, ZFREE_STATFREE);
906 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
914 if (keg->uk_flags & UMA_ZONE_HASH)
915 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
917 keg->uk_pages += keg->uk_ppera;
918 keg->uk_free += keg->uk_ipers;
924 * This function is intended to be used early on in place of page_alloc() so
925 * that we may use the boot time page cache to satisfy allocations before
929 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
933 int pages, check_pages;
935 keg = zone_first_keg(zone);
936 pages = howmany(bytes, PAGE_SIZE);
937 check_pages = pages - 1;
938 KASSERT(pages > 0, ("startup_alloc can't reserve 0 pages\n"));
941 * Check our small startup cache to see if it has pages remaining.
943 mtx_lock(&uma_boot_pages_mtx);
945 /* First check if we have enough room. */
946 tmps = LIST_FIRST(&uma_boot_pages);
947 while (tmps != NULL && check_pages-- > 0)
948 tmps = LIST_NEXT(tmps, us_link);
951 * It's ok to lose tmps references. The last one will
952 * have tmps->us_data pointing to the start address of
953 * "pages" contiguous pages of memory.
955 while (pages-- > 0) {
956 tmps = LIST_FIRST(&uma_boot_pages);
957 LIST_REMOVE(tmps, us_link);
959 mtx_unlock(&uma_boot_pages_mtx);
960 *pflag = tmps->us_flags;
961 return (tmps->us_data);
963 mtx_unlock(&uma_boot_pages_mtx);
964 if (booted < UMA_STARTUP2)
965 panic("UMA: Increase vm.boot_pages");
967 * Now that we've booted reset these users to their real allocator.
969 #ifdef UMA_MD_SMALL_ALLOC
970 keg->uk_allocf = (keg->uk_ppera > 1) ? page_alloc : uma_small_alloc;
972 keg->uk_allocf = page_alloc;
974 return keg->uk_allocf(zone, bytes, pflag, wait);
978 * Allocates a number of pages from the system
981 * bytes The number of bytes requested
982 * wait Shall we wait?
985 * A pointer to the alloced memory or possibly
986 * NULL if M_NOWAIT is set.
989 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
991 void *p; /* Returned page */
993 *pflag = UMA_SLAB_KMEM;
994 p = (void *) kmem_malloc(kmem_map, bytes, wait);
1000 * Allocates a number of pages from within an object
1003 * bytes The number of bytes requested
1004 * wait Shall we wait?
1007 * A pointer to the alloced memory or possibly
1008 * NULL if M_NOWAIT is set.
1011 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
1014 vm_offset_t retkva, zkva;
1016 int pages, startpages;
1019 keg = zone_first_keg(zone);
1020 object = keg->uk_obj;
1024 * This looks a little weird since we're getting one page at a time.
1026 VM_OBJECT_LOCK(object);
1027 p = TAILQ_LAST(&object->memq, pglist);
1028 pages = p != NULL ? p->pindex + 1 : 0;
1030 zkva = keg->uk_kva + pages * PAGE_SIZE;
1031 for (; bytes > 0; bytes -= PAGE_SIZE) {
1032 p = vm_page_alloc(object, pages,
1033 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
1035 if (pages != startpages)
1036 pmap_qremove(retkva, pages - startpages);
1037 while (pages != startpages) {
1039 p = TAILQ_LAST(&object->memq, pglist);
1040 vm_page_unwire(p, 0);
1046 pmap_qenter(zkva, &p, 1);
1053 VM_OBJECT_UNLOCK(object);
1054 *flags = UMA_SLAB_PRIV;
1056 return ((void *)retkva);
1060 * Frees a number of pages to the system
1063 * mem A pointer to the memory to be freed
1064 * size The size of the memory being freed
1065 * flags The original p->us_flags field
1071 page_free(void *mem, int size, u_int8_t flags)
1075 if (flags & UMA_SLAB_KMEM)
1077 else if (flags & UMA_SLAB_KERNEL)
1080 panic("UMA: page_free used with invalid flags %d", flags);
1082 kmem_free(map, (vm_offset_t)mem, size);
1086 * Zero fill initializer
1088 * Arguments/Returns follow uma_init specifications
1091 zero_init(void *mem, int size, int flags)
1098 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1101 * keg The zone we should initialize
1107 keg_small_init(uma_keg_t keg)
1114 KASSERT(keg != NULL, ("Keg is null in keg_small_init"));
1115 rsize = keg->uk_size;
1117 if (rsize < UMA_SMALLEST_UNIT)
1118 rsize = UMA_SMALLEST_UNIT;
1119 if (rsize & keg->uk_align)
1120 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1122 keg->uk_rsize = rsize;
1125 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1127 } else if (keg->uk_flags & UMA_ZONE_REFCNT) {
1128 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1129 shsize = sizeof(struct uma_slab_refcnt);
1131 rsize += UMA_FRITM_SZ; /* Account for linkage */
1132 shsize = sizeof(struct uma_slab);
1135 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1136 KASSERT(keg->uk_ipers != 0, ("keg_small_init: ipers is 0"));
1137 memused = keg->uk_ipers * rsize + shsize;
1138 wastedspace = UMA_SLAB_SIZE - memused;
1141 * We can't do OFFPAGE if we're internal or if we've been
1142 * asked to not go to the VM for buckets. If we do this we
1143 * may end up going to the VM (kmem_map) for slabs which we
1144 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1145 * result of UMA_ZONE_VM, which clearly forbids it.
1147 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1148 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1151 if ((wastedspace >= UMA_MAX_WASTE) &&
1152 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1153 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1154 KASSERT(keg->uk_ipers <= 255,
1155 ("keg_small_init: keg->uk_ipers too high!"));
1157 printf("UMA decided we need offpage slab headers for "
1158 "keg: %s, calculated wastedspace = %d, "
1159 "maximum wasted space allowed = %d, "
1160 "calculated ipers = %d, "
1161 "new wasted space = %d\n", keg->uk_name, wastedspace,
1162 UMA_MAX_WASTE, keg->uk_ipers,
1163 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1165 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1166 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1167 keg->uk_flags |= UMA_ZONE_HASH;
1172 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1173 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1177 * keg The keg we should initialize
1183 keg_large_init(uma_keg_t keg)
1187 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1188 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1189 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1191 pages = keg->uk_size / UMA_SLAB_SIZE;
1193 /* Account for remainder */
1194 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1197 keg->uk_ppera = pages;
1199 keg->uk_rsize = keg->uk_size;
1201 /* We can't do OFFPAGE if we're internal, bail out here. */
1202 if (keg->uk_flags & UMA_ZFLAG_INTERNAL)
1205 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1206 if ((keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1207 keg->uk_flags |= UMA_ZONE_HASH;
1211 keg_cachespread_init(uma_keg_t keg)
1218 alignsize = keg->uk_align + 1;
1219 rsize = keg->uk_size;
1221 * We want one item to start on every align boundary in a page. To
1222 * do this we will span pages. We will also extend the item by the
1223 * size of align if it is an even multiple of align. Otherwise, it
1224 * would fall on the same boundary every time.
1226 if (rsize & keg->uk_align)
1227 rsize = (rsize & ~keg->uk_align) + alignsize;
1228 if ((rsize & alignsize) == 0)
1230 trailer = rsize - keg->uk_size;
1231 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1232 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1233 keg->uk_rsize = rsize;
1234 keg->uk_ppera = pages;
1235 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1236 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1237 KASSERT(keg->uk_ipers <= uma_max_ipers,
1238 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1243 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1244 * the keg onto the global keg list.
1246 * Arguments/Returns follow uma_ctor specifications
1247 * udata Actually uma_kctor_args
1250 keg_ctor(void *mem, int size, void *udata, int flags)
1252 struct uma_kctor_args *arg = udata;
1253 uma_keg_t keg = mem;
1257 keg->uk_size = arg->size;
1258 keg->uk_init = arg->uminit;
1259 keg->uk_fini = arg->fini;
1260 keg->uk_align = arg->align;
1263 keg->uk_flags = arg->flags;
1264 keg->uk_allocf = page_alloc;
1265 keg->uk_freef = page_free;
1266 keg->uk_recurse = 0;
1267 keg->uk_slabzone = NULL;
1270 * The master zone is passed to us at keg-creation time.
1273 keg->uk_name = zone->uz_name;
1275 if (arg->flags & UMA_ZONE_VM)
1276 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1278 if (arg->flags & UMA_ZONE_ZINIT)
1279 keg->uk_init = zero_init;
1281 if (arg->flags & UMA_ZONE_REFCNT || arg->flags & UMA_ZONE_MALLOC)
1282 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1285 * The +UMA_FRITM_SZ added to uk_size is to account for the
1286 * linkage that is added to the size in keg_small_init(). If
1287 * we don't account for this here then we may end up in
1288 * keg_small_init() with a calculated 'ipers' of 0.
1290 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1291 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1292 keg_cachespread_init(keg);
1293 else if ((keg->uk_size+UMA_FRITMREF_SZ) >
1294 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1295 keg_large_init(keg);
1297 keg_small_init(keg);
1299 if (keg->uk_flags & UMA_ZONE_CACHESPREAD)
1300 keg_cachespread_init(keg);
1301 else if ((keg->uk_size+UMA_FRITM_SZ) >
1302 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1303 keg_large_init(keg);
1305 keg_small_init(keg);
1308 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1309 if (keg->uk_flags & UMA_ZONE_REFCNT)
1310 keg->uk_slabzone = slabrefzone;
1312 keg->uk_slabzone = slabzone;
1316 * If we haven't booted yet we need allocations to go through the
1317 * startup cache until the vm is ready.
1319 if (keg->uk_ppera == 1) {
1320 #ifdef UMA_MD_SMALL_ALLOC
1321 keg->uk_allocf = uma_small_alloc;
1322 keg->uk_freef = uma_small_free;
1324 if (booted < UMA_STARTUP)
1325 keg->uk_allocf = startup_alloc;
1327 if (booted < UMA_STARTUP2)
1328 keg->uk_allocf = startup_alloc;
1330 } else if (booted < UMA_STARTUP2 &&
1331 (keg->uk_flags & UMA_ZFLAG_INTERNAL))
1332 keg->uk_allocf = startup_alloc;
1335 * Initialize keg's lock (shared among zones).
1337 if (arg->flags & UMA_ZONE_MTXCLASS)
1338 KEG_LOCK_INIT(keg, 1);
1340 KEG_LOCK_INIT(keg, 0);
1343 * If we're putting the slab header in the actual page we need to
1344 * figure out where in each page it goes. This calculates a right
1345 * justified offset into the memory on an ALIGN_PTR boundary.
1347 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1350 /* Size of the slab struct and free list */
1351 if (keg->uk_flags & UMA_ZONE_REFCNT)
1352 totsize = sizeof(struct uma_slab_refcnt) +
1353 keg->uk_ipers * UMA_FRITMREF_SZ;
1355 totsize = sizeof(struct uma_slab) +
1356 keg->uk_ipers * UMA_FRITM_SZ;
1358 if (totsize & UMA_ALIGN_PTR)
1359 totsize = (totsize & ~UMA_ALIGN_PTR) +
1360 (UMA_ALIGN_PTR + 1);
1361 keg->uk_pgoff = (UMA_SLAB_SIZE * keg->uk_ppera) - totsize;
1363 if (keg->uk_flags & UMA_ZONE_REFCNT)
1364 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1365 + keg->uk_ipers * UMA_FRITMREF_SZ;
1367 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1368 + keg->uk_ipers * UMA_FRITM_SZ;
1371 * The only way the following is possible is if with our
1372 * UMA_ALIGN_PTR adjustments we are now bigger than
1373 * UMA_SLAB_SIZE. I haven't checked whether this is
1374 * mathematically possible for all cases, so we make
1377 if (totsize > UMA_SLAB_SIZE * keg->uk_ppera) {
1378 printf("zone %s ipers %d rsize %d size %d\n",
1379 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1381 panic("UMA slab won't fit.");
1385 if (keg->uk_flags & UMA_ZONE_HASH)
1386 hash_alloc(&keg->uk_hash);
1389 printf("UMA: %s(%p) size %d(%d) flags %#x ipers %d ppera %d out %d free %d\n",
1390 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
1391 keg->uk_ipers, keg->uk_ppera,
1392 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
1395 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1398 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1399 mtx_unlock(&uma_mtx);
1404 * Zone header ctor. This initializes all fields, locks, etc.
1406 * Arguments/Returns follow uma_ctor specifications
1407 * udata Actually uma_zctor_args
1410 zone_ctor(void *mem, int size, void *udata, int flags)
1412 struct uma_zctor_args *arg = udata;
1413 uma_zone_t zone = mem;
1418 zone->uz_name = arg->name;
1419 zone->uz_ctor = arg->ctor;
1420 zone->uz_dtor = arg->dtor;
1421 zone->uz_slab = zone_fetch_slab;
1422 zone->uz_init = NULL;
1423 zone->uz_fini = NULL;
1424 zone->uz_allocs = 0;
1427 zone->uz_sleeps = 0;
1428 zone->uz_fills = zone->uz_count = 0;
1432 if (arg->flags & UMA_ZONE_SECONDARY) {
1433 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1434 zone->uz_init = arg->uminit;
1435 zone->uz_fini = arg->fini;
1436 zone->uz_lock = &keg->uk_lock;
1437 zone->uz_flags |= UMA_ZONE_SECONDARY;
1440 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1441 if (LIST_NEXT(z, uz_link) == NULL) {
1442 LIST_INSERT_AFTER(z, zone, uz_link);
1447 mtx_unlock(&uma_mtx);
1448 } else if (keg == NULL) {
1449 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1450 arg->align, arg->flags)) == NULL)
1453 struct uma_kctor_args karg;
1456 /* We should only be here from uma_startup() */
1457 karg.size = arg->size;
1458 karg.uminit = arg->uminit;
1459 karg.fini = arg->fini;
1460 karg.align = arg->align;
1461 karg.flags = arg->flags;
1463 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1469 * Link in the first keg.
1471 zone->uz_klink.kl_keg = keg;
1472 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1473 zone->uz_lock = &keg->uk_lock;
1474 zone->uz_size = keg->uk_size;
1475 zone->uz_flags |= (keg->uk_flags &
1476 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1479 * Some internal zones don't have room allocated for the per cpu
1480 * caches. If we're internal, bail out here.
1482 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1483 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1484 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1488 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1489 zone->uz_count = BUCKET_MAX;
1490 else if (keg->uk_ipers <= BUCKET_MAX)
1491 zone->uz_count = keg->uk_ipers;
1493 zone->uz_count = BUCKET_MAX;
1498 * Keg header dtor. This frees all data, destroys locks, frees the hash
1499 * table and removes the keg from the global list.
1501 * Arguments/Returns follow uma_dtor specifications
1505 keg_dtor(void *arg, int size, void *udata)
1509 keg = (uma_keg_t)arg;
1511 if (keg->uk_free != 0) {
1512 printf("Freed UMA keg was not empty (%d items). "
1513 " Lost %d pages of memory.\n",
1514 keg->uk_free, keg->uk_pages);
1518 hash_free(&keg->uk_hash);
1526 * Arguments/Returns follow uma_dtor specifications
1530 zone_dtor(void *arg, int size, void *udata)
1536 zone = (uma_zone_t)arg;
1537 keg = zone_first_keg(zone);
1539 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1543 LIST_REMOVE(zone, uz_link);
1544 mtx_unlock(&uma_mtx);
1546 * XXX there are some races here where
1547 * the zone can be drained but zone lock
1548 * released and then refilled before we
1549 * remove it... we dont care for now
1551 zone_drain_wait(zone, M_WAITOK);
1553 * Unlink all of our kegs.
1555 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1556 klink->kl_keg = NULL;
1557 LIST_REMOVE(klink, kl_link);
1558 if (klink == &zone->uz_klink)
1560 free(klink, M_TEMP);
1563 * We only destroy kegs from non secondary zones.
1565 if ((zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1567 LIST_REMOVE(keg, uk_link);
1568 mtx_unlock(&uma_mtx);
1569 zone_free_item(kegs, keg, NULL, SKIP_NONE,
1575 * Traverses every zone in the system and calls a callback
1578 * zfunc A pointer to a function which accepts a zone
1585 zone_foreach(void (*zfunc)(uma_zone_t))
1591 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1592 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1595 mtx_unlock(&uma_mtx);
1598 /* Public functions */
1601 uma_startup(void *bootmem, int boot_pages)
1603 struct uma_zctor_args args;
1606 u_int objsize, totsize, wsize;
1610 printf("Creating uma keg headers zone and keg.\n");
1612 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF);
1615 * Figure out the maximum number of items-per-slab we'll have if
1616 * we're using the OFFPAGE slab header to track free items, given
1617 * all possible object sizes and the maximum desired wastage
1620 * We iterate until we find an object size for
1621 * which the calculated wastage in keg_small_init() will be
1622 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1623 * is an overall increasing see-saw function, we find the smallest
1624 * objsize such that the wastage is always acceptable for objects
1625 * with that objsize or smaller. Since a smaller objsize always
1626 * generates a larger possible uma_max_ipers, we use this computed
1627 * objsize to calculate the largest ipers possible. Since the
1628 * ipers calculated for OFFPAGE slab headers is always larger than
1629 * the ipers initially calculated in keg_small_init(), we use
1630 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1631 * obtain the maximum ipers possible for offpage slab headers.
1633 * It should be noted that ipers versus objsize is an inversly
1634 * proportional function which drops off rather quickly so as
1635 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1636 * falls into the portion of the inverse relation AFTER the steep
1637 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1639 * Note that we have 8-bits (1 byte) to use as a freelist index
1640 * inside the actual slab header itself and this is enough to
1641 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1642 * object with offpage slab header would have ipers =
1643 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1644 * 1 greater than what our byte-integer freelist index can
1645 * accomodate, but we know that this situation never occurs as
1646 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1647 * that we need to go to offpage slab headers. Or, if we do,
1648 * then we trap that condition below and panic in the INVARIANTS case.
1650 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1652 objsize = UMA_SMALLEST_UNIT;
1653 while (totsize >= wsize) {
1654 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1655 (objsize + UMA_FRITM_SZ);
1656 totsize *= (UMA_FRITM_SZ + objsize);
1659 if (objsize > UMA_SMALLEST_UNIT)
1661 uma_max_ipers = MAX(UMA_SLAB_SIZE / objsize, 64);
1663 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1665 objsize = UMA_SMALLEST_UNIT;
1666 while (totsize >= wsize) {
1667 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1668 (objsize + UMA_FRITMREF_SZ);
1669 totsize *= (UMA_FRITMREF_SZ + objsize);
1672 if (objsize > UMA_SMALLEST_UNIT)
1674 uma_max_ipers_ref = MAX(UMA_SLAB_SIZE / objsize, 64);
1676 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1677 ("uma_startup: calculated uma_max_ipers values too large!"));
1680 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1681 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1685 /* "manually" create the initial zone */
1686 args.name = "UMA Kegs";
1687 args.size = sizeof(struct uma_keg);
1688 args.ctor = keg_ctor;
1689 args.dtor = keg_dtor;
1690 args.uminit = zero_init;
1692 args.keg = &masterkeg;
1693 args.align = 32 - 1;
1694 args.flags = UMA_ZFLAG_INTERNAL;
1695 /* The initial zone has no Per cpu queues so it's smaller */
1696 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1699 printf("Filling boot free list.\n");
1701 for (i = 0; i < boot_pages; i++) {
1702 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1703 slab->us_data = (u_int8_t *)slab;
1704 slab->us_flags = UMA_SLAB_BOOT;
1705 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1707 mtx_init(&uma_boot_pages_mtx, "UMA boot pages", NULL, MTX_DEF);
1710 printf("Creating uma zone headers zone and keg.\n");
1712 args.name = "UMA Zones";
1713 args.size = sizeof(struct uma_zone) +
1714 (sizeof(struct uma_cache) * (mp_maxid + 1));
1715 args.ctor = zone_ctor;
1716 args.dtor = zone_dtor;
1717 args.uminit = zero_init;
1720 args.align = 32 - 1;
1721 args.flags = UMA_ZFLAG_INTERNAL;
1722 /* The initial zone has no Per cpu queues so it's smaller */
1723 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1726 printf("Initializing pcpu cache locks.\n");
1729 printf("Creating slab and hash zones.\n");
1733 * This is the max number of free list items we'll have with
1736 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1737 slabsize += sizeof(struct uma_slab);
1739 /* Now make a zone for slab headers */
1740 slabzone = uma_zcreate("UMA Slabs",
1742 NULL, NULL, NULL, NULL,
1743 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1746 * We also create a zone for the bigger slabs with reference
1747 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1749 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1750 slabsize += sizeof(struct uma_slab_refcnt);
1751 slabrefzone = uma_zcreate("UMA RCntSlabs",
1753 NULL, NULL, NULL, NULL,
1755 UMA_ZFLAG_INTERNAL);
1757 hashzone = uma_zcreate("UMA Hash",
1758 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1759 NULL, NULL, NULL, NULL,
1760 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1764 booted = UMA_STARTUP;
1767 printf("UMA startup complete.\n");
1775 booted = UMA_STARTUP2;
1778 printf("UMA startup2 complete.\n");
1783 * Initialize our callout handle
1791 printf("Starting callout.\n");
1793 callout_init(&uma_callout, CALLOUT_MPSAFE);
1794 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1796 printf("UMA startup3 complete.\n");
1801 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1802 int align, u_int32_t flags)
1804 struct uma_kctor_args args;
1807 args.uminit = uminit;
1809 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
1812 return (zone_alloc_item(kegs, &args, M_WAITOK));
1817 uma_set_align(int align)
1820 if (align != UMA_ALIGN_CACHE)
1821 uma_align_cache = align;
1826 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1827 uma_init uminit, uma_fini fini, int align, u_int32_t flags)
1830 struct uma_zctor_args args;
1832 /* This stuff is essential for the zone ctor */
1837 args.uminit = uminit;
1843 return (zone_alloc_item(zones, &args, M_WAITOK));
1848 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1849 uma_init zinit, uma_fini zfini, uma_zone_t master)
1851 struct uma_zctor_args args;
1854 keg = zone_first_keg(master);
1856 args.size = keg->uk_size;
1859 args.uminit = zinit;
1861 args.align = keg->uk_align;
1862 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
1865 /* XXX Attaches only one keg of potentially many. */
1866 return (zone_alloc_item(zones, &args, M_WAITOK));
1870 zone_lock_pair(uma_zone_t a, uma_zone_t b)
1874 mtx_lock_flags(b->uz_lock, MTX_DUPOK);
1877 mtx_lock_flags(a->uz_lock, MTX_DUPOK);
1882 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
1890 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
1897 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
1899 zone_lock_pair(zone, master);
1901 * zone must use vtoslab() to resolve objects and must already be
1904 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
1905 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
1910 * The new master must also use vtoslab().
1912 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
1917 * Both must either be refcnt, or not be refcnt.
1919 if ((zone->uz_flags & UMA_ZONE_REFCNT) !=
1920 (master->uz_flags & UMA_ZONE_REFCNT)) {
1925 * The underlying object must be the same size. rsize
1928 if (master->uz_size != zone->uz_size) {
1933 * Put it at the end of the list.
1935 klink->kl_keg = zone_first_keg(master);
1936 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
1937 if (LIST_NEXT(kl, kl_link) == NULL) {
1938 LIST_INSERT_AFTER(kl, klink, kl_link);
1943 zone->uz_flags |= UMA_ZFLAG_MULTI;
1944 zone->uz_slab = zone_fetch_slab_multi;
1947 zone_unlock_pair(zone, master);
1949 free(klink, M_TEMP);
1957 uma_zdestroy(uma_zone_t zone)
1960 zone_free_item(zones, zone, NULL, SKIP_NONE, ZFREE_STATFREE);
1965 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1969 uma_bucket_t bucket;
1972 /* This is the fast path allocation */
1973 #ifdef UMA_DEBUG_ALLOC_1
1974 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1976 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1977 zone->uz_name, flags);
1979 if (flags & M_WAITOK) {
1980 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1981 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
1985 * If possible, allocate from the per-CPU cache. There are two
1986 * requirements for safe access to the per-CPU cache: (1) the thread
1987 * accessing the cache must not be preempted or yield during access,
1988 * and (2) the thread must not migrate CPUs without switching which
1989 * cache it accesses. We rely on a critical section to prevent
1990 * preemption and migration. We release the critical section in
1991 * order to acquire the zone mutex if we are unable to allocate from
1992 * the current cache; when we re-acquire the critical section, we
1993 * must detect and handle migration if it has occurred.
1998 cache = &zone->uz_cpu[cpu];
2001 bucket = cache->uc_allocbucket;
2004 if (bucket->ub_cnt > 0) {
2006 item = bucket->ub_bucket[bucket->ub_cnt];
2008 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2010 KASSERT(item != NULL,
2011 ("uma_zalloc: Bucket pointer mangled."));
2016 uma_dbg_alloc(zone, NULL, item);
2019 if (zone->uz_ctor != NULL) {
2020 if (zone->uz_ctor(item, zone->uz_size,
2021 udata, flags) != 0) {
2022 zone_free_item(zone, item, udata,
2023 SKIP_DTOR, ZFREE_STATFAIL |
2029 bzero(item, zone->uz_size);
2031 } else if (cache->uc_freebucket) {
2033 * We have run out of items in our allocbucket.
2034 * See if we can switch with our free bucket.
2036 if (cache->uc_freebucket->ub_cnt > 0) {
2037 #ifdef UMA_DEBUG_ALLOC
2038 printf("uma_zalloc: Swapping empty with"
2041 bucket = cache->uc_freebucket;
2042 cache->uc_freebucket = cache->uc_allocbucket;
2043 cache->uc_allocbucket = bucket;
2050 * Attempt to retrieve the item from the per-CPU cache has failed, so
2051 * we must go back to the zone. This requires the zone lock, so we
2052 * must drop the critical section, then re-acquire it when we go back
2053 * to the cache. Since the critical section is released, we may be
2054 * preempted or migrate. As such, make sure not to maintain any
2055 * thread-local state specific to the cache from prior to releasing
2056 * the critical section.
2062 cache = &zone->uz_cpu[cpu];
2063 bucket = cache->uc_allocbucket;
2064 if (bucket != NULL) {
2065 if (bucket->ub_cnt > 0) {
2069 bucket = cache->uc_freebucket;
2070 if (bucket != NULL && bucket->ub_cnt > 0) {
2076 /* Since we have locked the zone we may as well send back our stats */
2077 zone->uz_allocs += cache->uc_allocs;
2078 cache->uc_allocs = 0;
2079 zone->uz_frees += cache->uc_frees;
2080 cache->uc_frees = 0;
2082 /* Our old one is now a free bucket */
2083 if (cache->uc_allocbucket) {
2084 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
2085 ("uma_zalloc_arg: Freeing a non free bucket."));
2086 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2087 cache->uc_allocbucket, ub_link);
2088 cache->uc_allocbucket = NULL;
2091 /* Check the free list for a new alloc bucket */
2092 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
2093 KASSERT(bucket->ub_cnt != 0,
2094 ("uma_zalloc_arg: Returning an empty bucket."));
2096 LIST_REMOVE(bucket, ub_link);
2097 cache->uc_allocbucket = bucket;
2101 /* We are no longer associated with this CPU. */
2104 /* Bump up our uz_count so we get here less */
2105 if (zone->uz_count < BUCKET_MAX)
2109 * Now lets just fill a bucket and put it on the free list. If that
2110 * works we'll restart the allocation from the begining.
2112 if (zone_alloc_bucket(zone, flags)) {
2114 goto zalloc_restart;
2118 * We may not be able to get a bucket so return an actual item.
2121 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
2124 item = zone_alloc_item(zone, udata, flags);
2129 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int flags)
2133 mtx_assert(&keg->uk_lock, MA_OWNED);
2138 * Find a slab with some space. Prefer slabs that are partially
2139 * used over those that are totally full. This helps to reduce
2142 if (keg->uk_free != 0) {
2143 if (!LIST_EMPTY(&keg->uk_part_slab)) {
2144 slab = LIST_FIRST(&keg->uk_part_slab);
2146 slab = LIST_FIRST(&keg->uk_free_slab);
2147 LIST_REMOVE(slab, us_link);
2148 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
2151 MPASS(slab->us_keg == keg);
2156 * M_NOVM means don't ask at all!
2161 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2162 keg->uk_flags |= UMA_ZFLAG_FULL;
2164 * If this is not a multi-zone, set the FULL bit.
2165 * Otherwise slab_multi() takes care of it.
2167 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0)
2168 zone->uz_flags |= UMA_ZFLAG_FULL;
2169 if (flags & M_NOWAIT)
2172 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2176 slab = keg_alloc_slab(keg, zone, flags);
2179 * If we got a slab here it's safe to mark it partially used
2180 * and return. We assume that the caller is going to remove
2181 * at least one item.
2184 MPASS(slab->us_keg == keg);
2185 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2189 * We might not have been able to get a slab but another cpu
2190 * could have while we were unlocked. Check again before we
2199 zone_relock(uma_zone_t zone, uma_keg_t keg)
2201 if (zone->uz_lock != &keg->uk_lock) {
2208 keg_relock(uma_keg_t keg, uma_zone_t zone)
2210 if (zone->uz_lock != &keg->uk_lock) {
2217 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int flags)
2222 keg = zone_first_keg(zone);
2224 * This is to prevent us from recursively trying to allocate
2225 * buckets. The problem is that if an allocation forces us to
2226 * grab a new bucket we will call page_alloc, which will go off
2227 * and cause the vm to allocate vm_map_entries. If we need new
2228 * buckets there too we will recurse in kmem_alloc and bad
2229 * things happen. So instead we return a NULL bucket, and make
2230 * the code that allocates buckets smart enough to deal with it
2232 if (keg->uk_flags & UMA_ZFLAG_BUCKET && keg->uk_recurse != 0)
2236 slab = keg_fetch_slab(keg, zone, flags);
2239 if (flags & (M_NOWAIT | M_NOVM))
2246 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2247 * with the keg locked. Caller must call zone_relock() afterwards if the
2248 * zone lock is required. On NULL the zone lock is held.
2250 * The last pointer is used to seed the search. It is not required.
2253 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int rflags)
2263 * Don't wait on the first pass. This will skip limit tests
2264 * as well. We don't want to block if we can find a provider
2267 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2269 * Use the last slab allocated as a hint for where to start
2273 slab = keg_fetch_slab(last, zone, flags);
2276 zone_relock(zone, last);
2280 * Loop until we have a slab incase of transient failures
2281 * while M_WAITOK is specified. I'm not sure this is 100%
2282 * required but we've done it for so long now.
2288 * Search the available kegs for slabs. Be careful to hold the
2289 * correct lock while calling into the keg layer.
2291 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2292 keg = klink->kl_keg;
2293 keg_relock(keg, zone);
2294 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2295 slab = keg_fetch_slab(keg, zone, flags);
2299 if (keg->uk_flags & UMA_ZFLAG_FULL)
2303 zone_relock(zone, keg);
2305 if (rflags & (M_NOWAIT | M_NOVM))
2309 * All kegs are full. XXX We can't atomically check all kegs
2310 * and sleep so just sleep for a short period and retry.
2312 if (full && !empty) {
2313 zone->uz_flags |= UMA_ZFLAG_FULL;
2315 msleep(zone, zone->uz_lock, PVM, "zonelimit", hz/100);
2316 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2324 slab_alloc_item(uma_zone_t zone, uma_slab_t slab)
2327 uma_slabrefcnt_t slabref;
2332 mtx_assert(&keg->uk_lock, MA_OWNED);
2334 freei = slab->us_firstfree;
2335 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2336 slabref = (uma_slabrefcnt_t)slab;
2337 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2339 slab->us_firstfree = slab->us_freelist[freei].us_item;
2341 item = slab->us_data + (keg->uk_rsize * freei);
2343 slab->us_freecount--;
2346 uma_dbg_alloc(zone, slab, item);
2348 /* Move this slab to the full list */
2349 if (slab->us_freecount == 0) {
2350 LIST_REMOVE(slab, us_link);
2351 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2358 zone_alloc_bucket(uma_zone_t zone, int flags)
2360 uma_bucket_t bucket;
2364 int max, origflags = flags;
2367 * Try this zone's free list first so we don't allocate extra buckets.
2369 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2370 KASSERT(bucket->ub_cnt == 0,
2371 ("zone_alloc_bucket: Bucket on free list is not empty."));
2372 LIST_REMOVE(bucket, ub_link);
2376 bflags = (flags & ~M_ZERO);
2377 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2381 bucket = bucket_alloc(zone->uz_count, bflags);
2385 if (bucket == NULL) {
2391 * This code is here to limit the number of simultaneous bucket fills
2392 * for any given zone to the number of per cpu caches in this zone. This
2393 * is done so that we don't allocate more memory than we really need.
2395 if (zone->uz_fills >= mp_ncpus)
2401 max = MIN(bucket->ub_entries, zone->uz_count);
2402 /* Try to keep the buckets totally full */
2403 saved = bucket->ub_cnt;
2406 while (bucket->ub_cnt < max &&
2407 (slab = zone->uz_slab(zone, keg, flags)) != NULL) {
2409 while (slab->us_freecount && bucket->ub_cnt < max) {
2410 bucket->ub_bucket[bucket->ub_cnt++] =
2411 slab_alloc_item(zone, slab);
2414 /* Don't block on the next fill */
2418 zone_relock(zone, keg);
2421 * We unlock here because we need to call the zone's init.
2422 * It should be safe to unlock because the slab dealt with
2423 * above is already on the appropriate list within the keg
2424 * and the bucket we filled is not yet on any list, so we
2427 if (zone->uz_init != NULL) {
2431 for (i = saved; i < bucket->ub_cnt; i++)
2432 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2436 * If we couldn't initialize the whole bucket, put the
2437 * rest back onto the freelist.
2439 if (i != bucket->ub_cnt) {
2442 for (j = i; j < bucket->ub_cnt; j++) {
2443 zone_free_item(zone, bucket->ub_bucket[j],
2444 NULL, SKIP_FINI, 0);
2446 bucket->ub_bucket[j] = NULL;
2455 if (bucket->ub_cnt != 0) {
2456 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2463 bucket_free(bucket);
2468 * Allocates an item for an internal zone
2471 * zone The zone to alloc for.
2472 * udata The data to be passed to the constructor.
2473 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2476 * NULL if there is no memory and M_NOWAIT is set
2477 * An item if successful
2481 zone_alloc_item(uma_zone_t zone, void *udata, int flags)
2488 #ifdef UMA_DEBUG_ALLOC
2489 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2493 slab = zone->uz_slab(zone, NULL, flags);
2500 item = slab_alloc_item(zone, slab);
2502 zone_relock(zone, slab->us_keg);
2507 * We have to call both the zone's init (not the keg's init)
2508 * and the zone's ctor. This is because the item is going from
2509 * a keg slab directly to the user, and the user is expecting it
2510 * to be both zone-init'd as well as zone-ctor'd.
2512 if (zone->uz_init != NULL) {
2513 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2514 zone_free_item(zone, item, udata, SKIP_FINI,
2515 ZFREE_STATFAIL | ZFREE_STATFREE);
2519 if (zone->uz_ctor != NULL) {
2520 if (zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2521 zone_free_item(zone, item, udata, SKIP_DTOR,
2522 ZFREE_STATFAIL | ZFREE_STATFREE);
2527 bzero(item, zone->uz_size);
2534 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2537 uma_bucket_t bucket;
2541 #ifdef UMA_DEBUG_ALLOC_1
2542 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2544 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2547 /* uma_zfree(..., NULL) does nothing, to match free(9). */
2552 zone->uz_dtor(item, zone->uz_size, udata);
2556 if (zone->uz_flags & UMA_ZONE_MALLOC)
2557 uma_dbg_free(zone, udata, item);
2559 uma_dbg_free(zone, NULL, item);
2563 * The race here is acceptable. If we miss it we'll just have to wait
2564 * a little longer for the limits to be reset.
2566 if (zone->uz_flags & UMA_ZFLAG_FULL)
2567 goto zfree_internal;
2570 * If possible, free to the per-CPU cache. There are two
2571 * requirements for safe access to the per-CPU cache: (1) the thread
2572 * accessing the cache must not be preempted or yield during access,
2573 * and (2) the thread must not migrate CPUs without switching which
2574 * cache it accesses. We rely on a critical section to prevent
2575 * preemption and migration. We release the critical section in
2576 * order to acquire the zone mutex if we are unable to free to the
2577 * current cache; when we re-acquire the critical section, we must
2578 * detect and handle migration if it has occurred.
2583 cache = &zone->uz_cpu[cpu];
2586 bucket = cache->uc_freebucket;
2590 * Do we have room in our bucket? It is OK for this uz count
2591 * check to be slightly out of sync.
2594 if (bucket->ub_cnt < bucket->ub_entries) {
2595 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2596 ("uma_zfree: Freeing to non free bucket index."));
2597 bucket->ub_bucket[bucket->ub_cnt] = item;
2602 } else if (cache->uc_allocbucket) {
2603 #ifdef UMA_DEBUG_ALLOC
2604 printf("uma_zfree: Swapping buckets.\n");
2607 * We have run out of space in our freebucket.
2608 * See if we can switch with our alloc bucket.
2610 if (cache->uc_allocbucket->ub_cnt <
2611 cache->uc_freebucket->ub_cnt) {
2612 bucket = cache->uc_freebucket;
2613 cache->uc_freebucket = cache->uc_allocbucket;
2614 cache->uc_allocbucket = bucket;
2620 * We can get here for two reasons:
2622 * 1) The buckets are NULL
2623 * 2) The alloc and free buckets are both somewhat full.
2625 * We must go back the zone, which requires acquiring the zone lock,
2626 * which in turn means we must release and re-acquire the critical
2627 * section. Since the critical section is released, we may be
2628 * preempted or migrate. As such, make sure not to maintain any
2629 * thread-local state specific to the cache from prior to releasing
2630 * the critical section.
2636 cache = &zone->uz_cpu[cpu];
2637 if (cache->uc_freebucket != NULL) {
2638 if (cache->uc_freebucket->ub_cnt <
2639 cache->uc_freebucket->ub_entries) {
2643 if (cache->uc_allocbucket != NULL &&
2644 (cache->uc_allocbucket->ub_cnt <
2645 cache->uc_freebucket->ub_cnt)) {
2651 /* Since we have locked the zone we may as well send back our stats */
2652 zone->uz_allocs += cache->uc_allocs;
2653 cache->uc_allocs = 0;
2654 zone->uz_frees += cache->uc_frees;
2655 cache->uc_frees = 0;
2657 bucket = cache->uc_freebucket;
2658 cache->uc_freebucket = NULL;
2660 /* Can we throw this on the zone full list? */
2661 if (bucket != NULL) {
2662 #ifdef UMA_DEBUG_ALLOC
2663 printf("uma_zfree: Putting old bucket on the free list.\n");
2665 /* ub_cnt is pointing to the last free item */
2666 KASSERT(bucket->ub_cnt != 0,
2667 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2668 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2671 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2672 LIST_REMOVE(bucket, ub_link);
2674 cache->uc_freebucket = bucket;
2677 /* We are no longer associated with this CPU. */
2680 /* And the zone.. */
2683 #ifdef UMA_DEBUG_ALLOC
2684 printf("uma_zfree: Allocating new free bucket.\n");
2688 if (zone->uz_flags & UMA_ZFLAG_CACHEONLY)
2690 bucket = bucket_alloc(zone->uz_count, bflags);
2693 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2700 * If nothing else caught this, we'll just do an internal free.
2703 zone_free_item(zone, item, udata, SKIP_DTOR, ZFREE_STATFREE);
2709 * Frees an item to an INTERNAL zone or allocates a free bucket
2712 * zone The zone to free to
2713 * item The item we're freeing
2714 * udata User supplied data for the dtor
2715 * skip Skip dtors and finis
2718 zone_free_item(uma_zone_t zone, void *item, void *udata,
2719 enum zfreeskip skip, int flags)
2722 uma_slabrefcnt_t slabref;
2728 if (skip < SKIP_DTOR && zone->uz_dtor)
2729 zone->uz_dtor(item, zone->uz_size, udata);
2731 if (skip < SKIP_FINI && zone->uz_fini)
2732 zone->uz_fini(item, zone->uz_size);
2736 if (flags & ZFREE_STATFAIL)
2738 if (flags & ZFREE_STATFREE)
2741 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
2742 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2743 keg = zone_first_keg(zone); /* Must only be one. */
2744 if (zone->uz_flags & UMA_ZONE_HASH) {
2745 slab = hash_sfind(&keg->uk_hash, mem);
2747 mem += keg->uk_pgoff;
2748 slab = (uma_slab_t)mem;
2751 /* This prevents redundant lookups via free(). */
2752 if ((zone->uz_flags & UMA_ZONE_MALLOC) && udata != NULL)
2753 slab = (uma_slab_t)udata;
2755 slab = vtoslab((vm_offset_t)item);
2757 keg_relock(keg, zone);
2759 MPASS(keg == slab->us_keg);
2761 /* Do we need to remove from any lists? */
2762 if (slab->us_freecount+1 == keg->uk_ipers) {
2763 LIST_REMOVE(slab, us_link);
2764 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2765 } else if (slab->us_freecount == 0) {
2766 LIST_REMOVE(slab, us_link);
2767 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2770 /* Slab management stuff */
2771 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2776 uma_dbg_free(zone, slab, item);
2779 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2780 slabref = (uma_slabrefcnt_t)slab;
2781 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2783 slab->us_freelist[freei].us_item = slab->us_firstfree;
2785 slab->us_firstfree = freei;
2786 slab->us_freecount++;
2788 /* Zone statistics */
2792 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2793 if (keg->uk_pages < keg->uk_maxpages) {
2794 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2799 * We can handle one more allocation. Since we're clearing ZFLAG_FULL,
2800 * wake up all procs blocked on pages. This should be uncommon, so
2801 * keeping this simple for now (rather than adding count of blocked
2807 zone_relock(zone, keg);
2808 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2817 uma_zone_set_max(uma_zone_t zone, int nitems)
2822 keg = zone_first_keg(zone);
2823 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
2824 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2825 keg->uk_maxpages += keg->uk_ppera;
2826 nitems = keg->uk_maxpages * keg->uk_ipers;
2834 uma_zone_get_max(uma_zone_t zone)
2840 keg = zone_first_keg(zone);
2841 nitems = keg->uk_maxpages * keg->uk_ipers;
2849 uma_zone_get_cur(uma_zone_t zone)
2855 nitems = zone->uz_allocs - zone->uz_frees;
2858 * See the comment in sysctl_vm_zone_stats() regarding the
2859 * safety of accessing the per-cpu caches. With the zone lock
2860 * held, it is safe, but can potentially result in stale data.
2862 nitems += zone->uz_cpu[i].uc_allocs -
2863 zone->uz_cpu[i].uc_frees;
2867 return (nitems < 0 ? 0 : nitems);
2872 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2877 keg = zone_first_keg(zone);
2878 KASSERT(keg->uk_pages == 0,
2879 ("uma_zone_set_init on non-empty keg"));
2880 keg->uk_init = uminit;
2886 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2891 keg = zone_first_keg(zone);
2892 KASSERT(keg->uk_pages == 0,
2893 ("uma_zone_set_fini on non-empty keg"));
2894 keg->uk_fini = fini;
2900 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2903 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2904 ("uma_zone_set_zinit on non-empty keg"));
2905 zone->uz_init = zinit;
2911 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2914 KASSERT(zone_first_keg(zone)->uk_pages == 0,
2915 ("uma_zone_set_zfini on non-empty keg"));
2916 zone->uz_fini = zfini;
2921 /* XXX uk_freef is not actually used with the zone locked */
2923 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2927 zone_first_keg(zone)->uk_freef = freef;
2932 /* XXX uk_allocf is not actually used with the zone locked */
2934 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2939 keg = zone_first_keg(zone);
2940 keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2941 keg->uk_allocf = allocf;
2947 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2953 keg = zone_first_keg(zone);
2954 pages = count / keg->uk_ipers;
2956 if (pages * keg->uk_ipers < count)
2959 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2964 obj = vm_object_allocate(OBJT_PHYS, pages);
2966 VM_OBJECT_LOCK_INIT(obj, "uma object");
2967 _vm_object_allocate(OBJT_PHYS, pages, obj);
2972 keg->uk_maxpages = pages;
2973 keg->uk_allocf = obj_alloc;
2974 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2981 uma_prealloc(uma_zone_t zone, int items)
2987 keg = zone_first_keg(zone);
2989 slabs = items / keg->uk_ipers;
2990 if (slabs * keg->uk_ipers < items)
2993 slab = keg_alloc_slab(keg, zone, M_WAITOK);
2996 MPASS(slab->us_keg == keg);
2997 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
3005 uma_find_refcnt(uma_zone_t zone, void *item)
3007 uma_slabrefcnt_t slabref;
3012 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
3014 keg = slabref->us_keg;
3015 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
3016 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
3017 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
3019 refcnt = &slabref->us_freelist[idx].us_refcnt;
3028 printf("UMA: vm asked us to release pages!\n");
3031 zone_foreach(zone_drain);
3033 * Some slabs may have been freed but this zone will be visited early
3034 * we visit again so that we can free pages that are empty once other
3035 * zones are drained. We have to do the same for buckets.
3037 zone_drain(slabzone);
3038 zone_drain(slabrefzone);
3039 bucket_zone_drain();
3044 uma_zone_exhausted(uma_zone_t zone)
3049 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3055 uma_zone_exhausted_nolock(uma_zone_t zone)
3057 return (zone->uz_flags & UMA_ZFLAG_FULL);
3061 uma_large_malloc(int size, int wait)
3067 slab = zone_alloc_item(slabzone, NULL, wait);
3070 mem = page_alloc(NULL, size, &flags, wait);
3072 vsetslab((vm_offset_t)mem, slab);
3073 slab->us_data = mem;
3074 slab->us_flags = flags | UMA_SLAB_MALLOC;
3075 slab->us_size = size;
3077 zone_free_item(slabzone, slab, NULL, SKIP_NONE,
3078 ZFREE_STATFAIL | ZFREE_STATFREE);
3085 uma_large_free(uma_slab_t slab)
3087 vsetobj((vm_offset_t)slab->us_data, kmem_object);
3088 page_free(slab->us_data, slab->us_size, slab->us_flags);
3089 zone_free_item(slabzone, slab, NULL, SKIP_NONE, ZFREE_STATFREE);
3093 uma_print_stats(void)
3095 zone_foreach(uma_print_zone);
3099 slab_print(uma_slab_t slab)
3101 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
3102 slab->us_keg, slab->us_data, slab->us_freecount,
3103 slab->us_firstfree);
3107 cache_print(uma_cache_t cache)
3109 printf("alloc: %p(%d), free: %p(%d)\n",
3110 cache->uc_allocbucket,
3111 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3112 cache->uc_freebucket,
3113 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3117 uma_print_keg(uma_keg_t keg)
3121 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3122 "out %d free %d limit %d\n",
3123 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3124 keg->uk_ipers, keg->uk_ppera,
3125 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free,
3126 (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3127 printf("Part slabs:\n");
3128 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
3130 printf("Free slabs:\n");
3131 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
3133 printf("Full slabs:\n");
3134 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
3139 uma_print_zone(uma_zone_t zone)
3145 printf("zone: %s(%p) size %d flags %#x\n",
3146 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3147 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3148 uma_print_keg(kl->kl_keg);
3150 cache = &zone->uz_cpu[i];
3151 printf("CPU %d Cache:\n", i);
3158 * Generate statistics across both the zone and its per-cpu cache's. Return
3159 * desired statistics if the pointer is non-NULL for that statistic.
3161 * Note: does not update the zone statistics, as it can't safely clear the
3162 * per-CPU cache statistic.
3164 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3165 * safe from off-CPU; we should modify the caches to track this information
3166 * directly so that we don't have to.
3169 uma_zone_sumstat(uma_zone_t z, int *cachefreep, u_int64_t *allocsp,
3170 u_int64_t *freesp, u_int64_t *sleepsp)
3173 u_int64_t allocs, frees, sleeps;
3176 allocs = frees = sleeps = 0;
3179 cache = &z->uz_cpu[cpu];
3180 if (cache->uc_allocbucket != NULL)
3181 cachefree += cache->uc_allocbucket->ub_cnt;
3182 if (cache->uc_freebucket != NULL)
3183 cachefree += cache->uc_freebucket->ub_cnt;
3184 allocs += cache->uc_allocs;
3185 frees += cache->uc_frees;
3187 allocs += z->uz_allocs;
3188 frees += z->uz_frees;
3189 sleeps += z->uz_sleeps;
3190 if (cachefreep != NULL)
3191 *cachefreep = cachefree;
3192 if (allocsp != NULL)
3196 if (sleepsp != NULL)
3202 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3210 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3211 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3214 mtx_unlock(&uma_mtx);
3215 return (sysctl_handle_int(oidp, &count, 0, req));
3219 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3221 struct uma_stream_header ush;
3222 struct uma_type_header uth;
3223 struct uma_percpu_stat ups;
3224 uma_bucket_t bucket;
3231 int count, error, i;
3233 error = sysctl_wire_old_buffer(req, 0);
3236 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3240 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3241 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3246 * Insert stream header.
3248 bzero(&ush, sizeof(ush));
3249 ush.ush_version = UMA_STREAM_VERSION;
3250 ush.ush_maxcpus = (mp_maxid + 1);
3251 ush.ush_count = count;
3252 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3254 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3255 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3256 bzero(&uth, sizeof(uth));
3258 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3259 uth.uth_align = kz->uk_align;
3260 uth.uth_size = kz->uk_size;
3261 uth.uth_rsize = kz->uk_rsize;
3262 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3264 uth.uth_maxpages += k->uk_maxpages;
3265 uth.uth_pages += k->uk_pages;
3266 uth.uth_keg_free += k->uk_free;
3267 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3272 * A zone is secondary is it is not the first entry
3273 * on the keg's zone list.
3275 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3276 (LIST_FIRST(&kz->uk_zones) != z))
3277 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3279 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3280 uth.uth_zone_free += bucket->ub_cnt;
3281 uth.uth_allocs = z->uz_allocs;
3282 uth.uth_frees = z->uz_frees;
3283 uth.uth_fails = z->uz_fails;
3284 uth.uth_sleeps = z->uz_sleeps;
3285 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
3287 * While it is not normally safe to access the cache
3288 * bucket pointers while not on the CPU that owns the
3289 * cache, we only allow the pointers to be exchanged
3290 * without the zone lock held, not invalidated, so
3291 * accept the possible race associated with bucket
3292 * exchange during monitoring.
3294 for (i = 0; i < (mp_maxid + 1); i++) {
3295 bzero(&ups, sizeof(ups));
3296 if (kz->uk_flags & UMA_ZFLAG_INTERNAL)
3300 cache = &z->uz_cpu[i];
3301 if (cache->uc_allocbucket != NULL)
3302 ups.ups_cache_free +=
3303 cache->uc_allocbucket->ub_cnt;
3304 if (cache->uc_freebucket != NULL)
3305 ups.ups_cache_free +=
3306 cache->uc_freebucket->ub_cnt;
3307 ups.ups_allocs = cache->uc_allocs;
3308 ups.ups_frees = cache->uc_frees;
3310 (void)sbuf_bcat(&sbuf, &ups, sizeof(ups));
3315 mtx_unlock(&uma_mtx);
3316 error = sbuf_finish(&sbuf);
3322 DB_SHOW_COMMAND(uma, db_show_uma)
3324 u_int64_t allocs, frees, sleeps;
3325 uma_bucket_t bucket;
3330 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
3331 "Requests", "Sleeps");
3332 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3333 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3334 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
3335 allocs = z->uz_allocs;
3336 frees = z->uz_frees;
3337 sleeps = z->uz_sleeps;
3340 uma_zone_sumstat(z, &cachefree, &allocs,
3342 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
3343 (LIST_FIRST(&kz->uk_zones) != z)))
3344 cachefree += kz->uk_free;
3345 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link)
3346 cachefree += bucket->ub_cnt;
3347 db_printf("%18s %8ju %8jd %8d %12ju %8ju\n", z->uz_name,
3348 (uintmax_t)kz->uk_size,
3349 (intmax_t)(allocs - frees), cachefree,
3350 (uintmax_t)allocs, sleeps);