2 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * uma_core.c Implementation of the Universal Memory allocator
30 * This allocator is intended to replace the multitude of similar object caches
31 * in the standard FreeBSD kernel. The intent is to be flexible as well as
32 * effecient. A primary design goal is to return unused memory to the rest of
33 * the system. This will make the system as a whole more flexible due to the
34 * ability to move memory to subsystems which most need it instead of leaving
35 * pools of reserved memory unused.
37 * The basic ideas stem from similar slab/zone based allocators whose algorithms
44 * - Improve memory usage for large allocations
45 * - Investigate cache size adjustments
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 /* I should really use ktr.. */
54 #define UMA_DEBUG_ALLOC 1
55 #define UMA_DEBUG_ALLOC_1 1
58 #include "opt_param.h"
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/types.h>
63 #include <sys/queue.h>
64 #include <sys/malloc.h>
67 #include <sys/sysctl.h>
68 #include <sys/mutex.h>
71 #include <sys/vmmeter.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_extern.h>
81 #include <vm/uma_int.h>
82 #include <vm/uma_dbg.h>
84 #include <machine/vmparam.h>
87 * This is the zone and keg from which all zones are spawned. The idea is that
88 * even the zone & keg heads are allocated from the allocator, so we use the
89 * bss section to bootstrap us.
91 static struct uma_keg masterkeg;
92 static struct uma_zone masterzone_k;
93 static struct uma_zone masterzone_z;
94 static uma_zone_t kegs = &masterzone_k;
95 static uma_zone_t zones = &masterzone_z;
97 /* This is the zone from which all of uma_slab_t's are allocated. */
98 static uma_zone_t slabzone;
99 static uma_zone_t slabrefzone; /* With refcounters (for UMA_ZONE_REFCNT) */
102 * The initial hash tables come out of this zone so they can be allocated
103 * prior to malloc coming up.
105 static uma_zone_t hashzone;
107 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
110 * Are we allowed to allocate buckets?
112 static int bucketdisable = 1;
114 /* Linked list of all kegs in the system */
115 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(&uma_kegs);
117 /* This mutex protects the keg list */
118 static struct mtx uma_mtx;
120 /* These are the pcpu cache locks */
121 static struct mtx uma_pcpu_mtx[MAXCPU];
123 /* Linked list of boot time pages */
124 static LIST_HEAD(,uma_slab) uma_boot_pages =
125 LIST_HEAD_INITIALIZER(&uma_boot_pages);
127 /* Count of free boottime pages */
128 static int uma_boot_free = 0;
130 /* Is the VM done starting up? */
131 static int booted = 0;
133 /* Maximum number of allowed items-per-slab if the slab header is OFFPAGE */
134 static u_int uma_max_ipers;
135 static u_int uma_max_ipers_ref;
138 * This is the handle used to schedule events that need to happen
139 * outside of the allocation fast path.
141 static struct callout uma_callout;
142 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
145 * This structure is passed as the zone ctor arg so that I don't have to create
146 * a special allocation function just for zones.
148 struct uma_zctor_args {
160 struct uma_kctor_args {
169 struct uma_bucket_zone {
175 #define BUCKET_MAX 128
177 struct uma_bucket_zone bucket_zones[] = {
178 { NULL, "16 Bucket", 16 },
179 { NULL, "32 Bucket", 32 },
180 { NULL, "64 Bucket", 64 },
181 { NULL, "128 Bucket", 128 },
185 #define BUCKET_SHIFT 4
186 #define BUCKET_ZONES ((BUCKET_MAX >> BUCKET_SHIFT) + 1)
188 uint8_t bucket_size[BUCKET_ZONES];
190 enum zfreeskip { SKIP_NONE, SKIP_DTOR, SKIP_FINI };
194 static void *obj_alloc(uma_zone_t, int, u_int8_t *, int);
195 static void *page_alloc(uma_zone_t, int, u_int8_t *, int);
196 static void *startup_alloc(uma_zone_t, int, u_int8_t *, int);
197 static void page_free(void *, int, u_int8_t);
198 static uma_slab_t slab_zalloc(uma_zone_t, int);
199 static void cache_drain(uma_zone_t);
200 static void bucket_drain(uma_zone_t, uma_bucket_t);
201 static void bucket_cache_drain(uma_zone_t zone);
202 static int keg_ctor(void *, int, void *, int);
203 static void keg_dtor(void *, int, void *);
204 static int zone_ctor(void *, int, void *, int);
205 static void zone_dtor(void *, int, void *);
206 static int zero_init(void *, int, int);
207 static void zone_small_init(uma_zone_t zone);
208 static void zone_large_init(uma_zone_t zone);
209 static void zone_foreach(void (*zfunc)(uma_zone_t));
210 static void zone_timeout(uma_zone_t zone);
211 static int hash_alloc(struct uma_hash *);
212 static int hash_expand(struct uma_hash *, struct uma_hash *);
213 static void hash_free(struct uma_hash *hash);
214 static void uma_timeout(void *);
215 static void uma_startup3(void);
216 static void *uma_zalloc_internal(uma_zone_t, void *, int);
217 static void uma_zfree_internal(uma_zone_t, void *, void *, enum zfreeskip);
218 static void bucket_enable(void);
219 static void bucket_init(void);
220 static uma_bucket_t bucket_alloc(int, int);
221 static void bucket_free(uma_bucket_t);
222 static void bucket_zone_drain(void);
223 static int uma_zalloc_bucket(uma_zone_t zone, int flags);
224 static uma_slab_t uma_zone_slab(uma_zone_t zone, int flags);
225 static void *uma_slab_alloc(uma_zone_t zone, uma_slab_t slab);
226 static void zone_drain(uma_zone_t);
227 static uma_zone_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
228 uma_fini fini, int align, u_int16_t flags);
230 void uma_print_zone(uma_zone_t);
231 void uma_print_stats(void);
232 static int sysctl_vm_zone(SYSCTL_HANDLER_ARGS);
235 static int nosleepwithlocks = 1;
236 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
237 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
239 static int nosleepwithlocks = 0;
240 SYSCTL_INT(_debug, OID_AUTO, nosleepwithlocks, CTLFLAG_RW, &nosleepwithlocks,
241 0, "Convert M_WAITOK to M_NOWAIT to avoid lock-held-across-sleep paths");
243 SYSCTL_OID(_vm, OID_AUTO, zone, CTLTYPE_STRING|CTLFLAG_RD,
244 NULL, 0, sysctl_vm_zone, "A", "Zone Info");
245 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
248 * This routine checks to see whether or not it's safe to enable buckets.
254 if (cnt.v_free_count < cnt.v_free_min)
263 struct uma_bucket_zone *ubz;
267 for (i = 0, j = 0; bucket_zones[j].ubz_entries != 0; j++) {
270 ubz = &bucket_zones[j];
271 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
272 size += sizeof(void *) * ubz->ubz_entries;
273 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
274 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
275 for (; i <= ubz->ubz_entries; i += (1 << BUCKET_SHIFT))
276 bucket_size[i >> BUCKET_SHIFT] = j;
281 bucket_alloc(int entries, int bflags)
283 struct uma_bucket_zone *ubz;
288 * This is to stop us from allocating per cpu buckets while we're
289 * running out of UMA_BOOT_PAGES. Otherwise, we would exhaust the
290 * boot pages. This also prevents us from allocating buckets in
291 * low memory situations.
296 idx = howmany(entries, 1 << BUCKET_SHIFT);
297 ubz = &bucket_zones[bucket_size[idx]];
298 bucket = uma_zalloc_internal(ubz->ubz_zone, NULL, bflags);
301 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
304 bucket->ub_entries = ubz->ubz_entries;
311 bucket_free(uma_bucket_t bucket)
313 struct uma_bucket_zone *ubz;
316 idx = howmany(bucket->ub_entries, 1 << BUCKET_SHIFT);
317 ubz = &bucket_zones[bucket_size[idx]];
318 uma_zfree_internal(ubz->ubz_zone, bucket, NULL, SKIP_NONE);
322 bucket_zone_drain(void)
324 struct uma_bucket_zone *ubz;
326 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
327 zone_drain(ubz->ubz_zone);
332 * Routine called by timeout which is used to fire off some time interval
333 * based calculations. (stats, hash size, etc.)
342 uma_timeout(void *unused)
345 zone_foreach(zone_timeout);
347 /* Reschedule this event */
348 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
352 * Routine to perform timeout driven calculations. This expands the
353 * hashes and does per cpu statistics aggregation.
356 * zone The zone to operate on
362 zone_timeout(uma_zone_t zone)
373 * Aggregate per cpu cache statistics back to the zone.
375 * XXX This should be done in the sysctl handler.
377 * I may rewrite this to set a flag in the per cpu cache instead of
378 * locking. If the flag is not cleared on the next round I will have
379 * to lock and do it here instead so that the statistics don't get too
382 if (!(keg->uk_flags & UMA_ZFLAG_INTERNAL)) {
383 for (cpu = 0; cpu <= mp_maxid; cpu++) {
387 cache = &zone->uz_cpu[cpu];
388 /* Add them up, and reset */
389 alloc += cache->uc_allocs;
390 cache->uc_allocs = 0;
395 /* Now push these stats back into the zone.. */
397 zone->uz_allocs += alloc;
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.
595 * zone The zone to drain, must be unlocked.
601 cache_drain(uma_zone_t zone)
607 * We have to lock each cpu cache before locking the zone
609 for (cpu = 0; cpu <= mp_maxid; cpu++) {
613 cache = &zone->uz_cpu[cpu];
614 bucket_drain(zone, cache->uc_allocbucket);
615 bucket_drain(zone, cache->uc_freebucket);
616 if (cache->uc_allocbucket != NULL)
617 bucket_free(cache->uc_allocbucket);
618 if (cache->uc_freebucket != NULL)
619 bucket_free(cache->uc_freebucket);
620 cache->uc_allocbucket = cache->uc_freebucket = NULL;
623 bucket_cache_drain(zone);
625 for (cpu = 0; cpu <= mp_maxid; cpu++) {
633 * Drain the cached buckets from a zone. Expects a locked zone on entry.
636 bucket_cache_drain(uma_zone_t zone)
641 * Drain the bucket queues and free the buckets, we just keep two per
644 while ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
645 LIST_REMOVE(bucket, ub_link);
647 bucket_drain(zone, bucket);
652 /* Now we do the free queue.. */
653 while ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
654 LIST_REMOVE(bucket, ub_link);
660 * Frees pages from a zone back to the system. This is done on demand from
661 * the pageout daemon.
664 * zone The zone to free pages from
665 * all Should we drain all items?
671 zone_drain(uma_zone_t zone)
673 struct slabhead freeslabs = {};
684 * We don't want to take pages from statically allocated zones at this
687 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
693 printf("%s free items: %u\n", zone->uz_name, keg->uk_free);
695 bucket_cache_drain(zone);
696 if (keg->uk_free == 0)
699 slab = LIST_FIRST(&keg->uk_free_slab);
701 n = LIST_NEXT(slab, us_link);
703 /* We have no where to free these to */
704 if (slab->us_flags & UMA_SLAB_BOOT) {
709 LIST_REMOVE(slab, us_link);
710 keg->uk_pages -= keg->uk_ppera;
711 keg->uk_free -= keg->uk_ipers;
713 if (keg->uk_flags & UMA_ZONE_HASH)
714 UMA_HASH_REMOVE(&keg->uk_hash, slab, slab->us_data);
716 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
723 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
724 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
726 for (i = 0; i < keg->uk_ipers; i++)
728 slab->us_data + (keg->uk_rsize * i),
730 flags = slab->us_flags;
733 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
734 (keg->uk_flags & UMA_ZONE_REFCNT)) {
737 if (flags & UMA_SLAB_KMEM)
741 for (i = 0; i < keg->uk_ppera; i++)
742 vsetobj((vm_offset_t)mem + (i * PAGE_SIZE),
745 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
746 uma_zfree_internal(keg->uk_slabzone, slab, NULL,
749 printf("%s: Returning %d bytes.\n",
750 zone->uz_name, UMA_SLAB_SIZE * keg->uk_ppera);
752 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera, flags);
757 * Allocate a new slab for a zone. This does not insert the slab onto a list.
760 * zone The zone to allocate slabs for
761 * wait Shall we wait?
764 * The slab that was allocated or NULL if there is no memory and the
765 * caller specified M_NOWAIT.
768 slab_zalloc(uma_zone_t zone, int wait)
770 uma_slabrefcnt_t slabref;
781 printf("slab_zalloc: Allocating a new slab for %s\n", zone->uz_name);
785 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
786 slab = uma_zalloc_internal(keg->uk_slabzone, NULL, wait);
794 * This reproduces the old vm_zone behavior of zero filling pages the
795 * first time they are added to a zone.
797 * Malloced items are zeroed in uma_zalloc.
800 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
805 mem = keg->uk_allocf(zone, keg->uk_ppera * UMA_SLAB_SIZE,
808 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
809 uma_zfree_internal(keg->uk_slabzone, slab, NULL, 0);
814 /* Point the slab into the allocated memory */
815 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
816 slab = (uma_slab_t )(mem + keg->uk_pgoff);
818 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
819 (keg->uk_flags & UMA_ZONE_REFCNT))
820 for (i = 0; i < keg->uk_ppera; i++)
821 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
825 slab->us_freecount = keg->uk_ipers;
826 slab->us_firstfree = 0;
827 slab->us_flags = flags;
829 if (keg->uk_flags & UMA_ZONE_REFCNT) {
830 slabref = (uma_slabrefcnt_t)slab;
831 for (i = 0; i < keg->uk_ipers; i++) {
832 slabref->us_freelist[i].us_refcnt = 0;
833 slabref->us_freelist[i].us_item = i+1;
836 for (i = 0; i < keg->uk_ipers; i++)
837 slab->us_freelist[i].us_item = i+1;
840 if (keg->uk_init != NULL) {
841 for (i = 0; i < keg->uk_ipers; i++)
842 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
843 keg->uk_size, wait) != 0)
845 if (i != keg->uk_ipers) {
846 if (keg->uk_fini != NULL) {
847 for (i--; i > -1; i--)
848 keg->uk_fini(slab->us_data +
852 if ((keg->uk_flags & UMA_ZONE_MALLOC) ||
853 (keg->uk_flags & UMA_ZONE_REFCNT))
854 for (i = 0; i < keg->uk_ppera; i++)
855 vsetobj((vm_offset_t)mem +
856 (i * PAGE_SIZE), NULL);
857 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
858 uma_zfree_internal(keg->uk_slabzone, slab,
860 keg->uk_freef(mem, UMA_SLAB_SIZE * keg->uk_ppera,
868 if (keg->uk_flags & UMA_ZONE_HASH)
869 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
871 keg->uk_pages += keg->uk_ppera;
872 keg->uk_free += keg->uk_ipers;
878 * This function is intended to be used early on in place of page_alloc() so
879 * that we may use the boot time page cache to satisfy allocations before
883 startup_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
890 * Check our small startup cache to see if it has pages remaining.
893 if (uma_boot_free != 0) {
896 tmps = LIST_FIRST(&uma_boot_pages);
897 LIST_REMOVE(tmps, us_link);
899 mtx_unlock(&uma_mtx);
900 *pflag = tmps->us_flags;
901 return (tmps->us_data);
903 mtx_unlock(&uma_mtx);
905 panic("UMA: Increase UMA_BOOT_PAGES");
907 * Now that we've booted reset these users to their real allocator.
909 #ifdef UMA_MD_SMALL_ALLOC
910 keg->uk_allocf = uma_small_alloc;
912 keg->uk_allocf = page_alloc;
914 return keg->uk_allocf(zone, bytes, pflag, wait);
918 * Allocates a number of pages from the system
922 * bytes The number of bytes requested
923 * wait Shall we wait?
926 * A pointer to the alloced memory or possibly
927 * NULL if M_NOWAIT is set.
930 page_alloc(uma_zone_t zone, int bytes, u_int8_t *pflag, int wait)
932 void *p; /* Returned page */
934 *pflag = UMA_SLAB_KMEM;
935 p = (void *) kmem_malloc(kmem_map, bytes, wait);
941 * Allocates a number of pages from within an object
945 * bytes The number of bytes requested
946 * wait Shall we wait?
949 * A pointer to the alloced memory or possibly
950 * NULL if M_NOWAIT is set.
953 obj_alloc(uma_zone_t zone, int bytes, u_int8_t *flags, int wait)
956 vm_offset_t retkva, zkva;
958 int pages, startpages;
960 object = zone->uz_keg->uk_obj;
964 * This looks a little weird since we're getting one page at a time.
966 VM_OBJECT_LOCK(object);
967 p = TAILQ_LAST(&object->memq, pglist);
968 pages = p != NULL ? p->pindex + 1 : 0;
970 zkva = zone->uz_keg->uk_kva + pages * PAGE_SIZE;
971 for (; bytes > 0; bytes -= PAGE_SIZE) {
972 p = vm_page_alloc(object, pages,
973 VM_ALLOC_INTERRUPT | VM_ALLOC_WIRED);
975 if (pages != startpages)
976 pmap_qremove(retkva, pages - startpages);
977 while (pages != startpages) {
979 p = TAILQ_LAST(&object->memq, pglist);
980 vm_page_lock_queues();
981 vm_page_unwire(p, 0);
983 vm_page_unlock_queues();
988 pmap_qenter(zkva, &p, 1);
995 VM_OBJECT_UNLOCK(object);
996 *flags = UMA_SLAB_PRIV;
998 return ((void *)retkva);
1002 * Frees a number of pages to the system
1005 * mem A pointer to the memory to be freed
1006 * size The size of the memory being freed
1007 * flags The original p->us_flags field
1013 page_free(void *mem, int size, u_int8_t flags)
1017 if (flags & UMA_SLAB_KMEM)
1020 panic("UMA: page_free used with invalid flags %d\n", flags);
1022 kmem_free(map, (vm_offset_t)mem, size);
1026 * Zero fill initializer
1028 * Arguments/Returns follow uma_init specifications
1031 zero_init(void *mem, int size, int flags)
1038 * Finish creating a small uma zone. This calculates ipers, and the zone size.
1041 * zone The zone we should initialize
1047 zone_small_init(uma_zone_t zone)
1056 KASSERT(keg != NULL, ("Keg is null in zone_small_init"));
1057 rsize = keg->uk_size;
1059 if (rsize < UMA_SMALLEST_UNIT)
1060 rsize = UMA_SMALLEST_UNIT;
1061 if (rsize & keg->uk_align)
1062 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1064 keg->uk_rsize = rsize;
1067 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1068 rsize += UMA_FRITMREF_SZ; /* linkage & refcnt */
1069 shsize = sizeof(struct uma_slab_refcnt);
1071 rsize += UMA_FRITM_SZ; /* Account for linkage */
1072 shsize = sizeof(struct uma_slab);
1075 keg->uk_ipers = (UMA_SLAB_SIZE - shsize) / rsize;
1076 KASSERT(keg->uk_ipers != 0, ("zone_small_init: ipers is 0"));
1077 memused = keg->uk_ipers * rsize + shsize;
1078 wastedspace = UMA_SLAB_SIZE - memused;
1081 * We can't do OFFPAGE if we're internal or if we've been
1082 * asked to not go to the VM for buckets. If we do this we
1083 * may end up going to the VM (kmem_map) for slabs which we
1084 * do not want to do if we're UMA_ZFLAG_CACHEONLY as a
1085 * result of UMA_ZONE_VM, which clearly forbids it.
1087 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1088 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1091 if ((wastedspace >= UMA_MAX_WASTE) &&
1092 (keg->uk_ipers < (UMA_SLAB_SIZE / keg->uk_rsize))) {
1093 keg->uk_ipers = UMA_SLAB_SIZE / keg->uk_rsize;
1094 KASSERT(keg->uk_ipers <= 255,
1095 ("zone_small_init: keg->uk_ipers too high!"));
1097 printf("UMA decided we need offpage slab headers for "
1098 "zone: %s, calculated wastedspace = %d, "
1099 "maximum wasted space allowed = %d, "
1100 "calculated ipers = %d, "
1101 "new wasted space = %d\n", zone->uz_name, wastedspace,
1102 UMA_MAX_WASTE, keg->uk_ipers,
1103 UMA_SLAB_SIZE - keg->uk_ipers * keg->uk_rsize);
1105 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1106 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1107 keg->uk_flags |= UMA_ZONE_HASH;
1112 * Finish creating a large (> UMA_SLAB_SIZE) uma zone. Just give in and do
1113 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1117 * zone The zone we should initialize
1123 zone_large_init(uma_zone_t zone)
1130 KASSERT(keg != NULL, ("Keg is null in zone_large_init"));
1131 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1132 ("zone_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY zone"));
1134 pages = keg->uk_size / UMA_SLAB_SIZE;
1136 /* Account for remainder */
1137 if ((pages * UMA_SLAB_SIZE) < keg->uk_size)
1140 keg->uk_ppera = pages;
1143 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1144 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1145 keg->uk_flags |= UMA_ZONE_HASH;
1147 keg->uk_rsize = keg->uk_size;
1151 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1152 * the keg onto the global keg list.
1154 * Arguments/Returns follow uma_ctor specifications
1155 * udata Actually uma_kctor_args
1158 keg_ctor(void *mem, int size, void *udata, int flags)
1160 struct uma_kctor_args *arg = udata;
1161 uma_keg_t keg = mem;
1165 keg->uk_size = arg->size;
1166 keg->uk_init = arg->uminit;
1167 keg->uk_fini = arg->fini;
1168 keg->uk_align = arg->align;
1171 keg->uk_flags = arg->flags;
1172 keg->uk_allocf = page_alloc;
1173 keg->uk_freef = page_free;
1174 keg->uk_recurse = 0;
1175 keg->uk_slabzone = NULL;
1178 * The master zone is passed to us at keg-creation time.
1183 if (arg->flags & UMA_ZONE_VM)
1184 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1186 if (arg->flags & UMA_ZONE_ZINIT)
1187 keg->uk_init = zero_init;
1190 * The +UMA_FRITM_SZ added to uk_size is to account for the
1191 * linkage that is added to the size in zone_small_init(). If
1192 * we don't account for this here then we may end up in
1193 * zone_small_init() with a calculated 'ipers' of 0.
1195 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1196 if ((keg->uk_size+UMA_FRITMREF_SZ) >
1197 (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)))
1198 zone_large_init(zone);
1200 zone_small_init(zone);
1202 if ((keg->uk_size+UMA_FRITM_SZ) >
1203 (UMA_SLAB_SIZE - sizeof(struct uma_slab)))
1204 zone_large_init(zone);
1206 zone_small_init(zone);
1209 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1210 if (keg->uk_flags & UMA_ZONE_REFCNT)
1211 keg->uk_slabzone = slabrefzone;
1213 keg->uk_slabzone = slabzone;
1217 * If we haven't booted yet we need allocations to go through the
1218 * startup cache until the vm is ready.
1220 if (keg->uk_ppera == 1) {
1221 #ifdef UMA_MD_SMALL_ALLOC
1222 keg->uk_allocf = uma_small_alloc;
1223 keg->uk_freef = uma_small_free;
1226 keg->uk_allocf = startup_alloc;
1230 * Initialize keg's lock (shared among zones) through
1233 zone->uz_lock = &keg->uk_lock;
1234 if (arg->flags & UMA_ZONE_MTXCLASS)
1235 ZONE_LOCK_INIT(zone, 1);
1237 ZONE_LOCK_INIT(zone, 0);
1240 * If we're putting the slab header in the actual page we need to
1241 * figure out where in each page it goes. This calculates a right
1242 * justified offset into the memory on an ALIGN_PTR boundary.
1244 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1247 /* Size of the slab struct and free list */
1248 if (keg->uk_flags & UMA_ZONE_REFCNT)
1249 totsize = sizeof(struct uma_slab_refcnt) +
1250 keg->uk_ipers * UMA_FRITMREF_SZ;
1252 totsize = sizeof(struct uma_slab) +
1253 keg->uk_ipers * UMA_FRITM_SZ;
1255 if (totsize & UMA_ALIGN_PTR)
1256 totsize = (totsize & ~UMA_ALIGN_PTR) +
1257 (UMA_ALIGN_PTR + 1);
1258 keg->uk_pgoff = UMA_SLAB_SIZE - totsize;
1260 if (keg->uk_flags & UMA_ZONE_REFCNT)
1261 totsize = keg->uk_pgoff + sizeof(struct uma_slab_refcnt)
1262 + keg->uk_ipers * UMA_FRITMREF_SZ;
1264 totsize = keg->uk_pgoff + sizeof(struct uma_slab)
1265 + keg->uk_ipers * UMA_FRITM_SZ;
1268 * The only way the following is possible is if with our
1269 * UMA_ALIGN_PTR adjustments we are now bigger than
1270 * UMA_SLAB_SIZE. I haven't checked whether this is
1271 * mathematically possible for all cases, so we make
1274 if (totsize > UMA_SLAB_SIZE) {
1275 printf("zone %s ipers %d rsize %d size %d\n",
1276 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1278 panic("UMA slab won't fit.\n");
1282 if (keg->uk_flags & UMA_ZONE_HASH)
1283 hash_alloc(&keg->uk_hash);
1286 printf("%s(%p) size = %d ipers = %d ppera = %d pgoff = %d\n",
1287 zone->uz_name, zone,
1288 keg->uk_size, keg->uk_ipers,
1289 keg->uk_ppera, keg->uk_pgoff);
1292 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1295 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1296 mtx_unlock(&uma_mtx);
1301 * Zone header ctor. This initializes all fields, locks, etc.
1303 * Arguments/Returns follow uma_ctor specifications
1304 * udata Actually uma_zctor_args
1308 zone_ctor(void *mem, int size, void *udata, int flags)
1310 struct uma_zctor_args *arg = udata;
1311 uma_zone_t zone = mem;
1316 zone->uz_name = arg->name;
1317 zone->uz_ctor = arg->ctor;
1318 zone->uz_dtor = arg->dtor;
1319 zone->uz_init = NULL;
1320 zone->uz_fini = NULL;
1321 zone->uz_allocs = 0;
1322 zone->uz_fills = zone->uz_count = 0;
1324 if (arg->flags & UMA_ZONE_SECONDARY) {
1325 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1328 zone->uz_init = arg->uminit;
1329 zone->uz_fini = arg->fini;
1330 zone->uz_lock = &keg->uk_lock;
1333 keg->uk_flags |= UMA_ZONE_SECONDARY;
1334 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1335 if (LIST_NEXT(z, uz_link) == NULL) {
1336 LIST_INSERT_AFTER(z, zone, uz_link);
1341 mtx_unlock(&uma_mtx);
1342 } else if (arg->keg == NULL) {
1343 if (uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1344 arg->align, arg->flags) == NULL)
1347 struct uma_kctor_args karg;
1350 /* We should only be here from uma_startup() */
1351 karg.size = arg->size;
1352 karg.uminit = arg->uminit;
1353 karg.fini = arg->fini;
1354 karg.align = arg->align;
1355 karg.flags = arg->flags;
1357 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1363 zone->uz_lock = &keg->uk_lock;
1366 * Some internal zones don't have room allocated for the per cpu
1367 * caches. If we're internal, bail out here.
1369 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1370 KASSERT((keg->uk_flags & UMA_ZONE_SECONDARY) == 0,
1371 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1375 if (keg->uk_flags & UMA_ZONE_MAXBUCKET)
1376 zone->uz_count = BUCKET_MAX;
1377 else if (keg->uk_ipers <= BUCKET_MAX)
1378 zone->uz_count = keg->uk_ipers;
1380 zone->uz_count = BUCKET_MAX;
1385 * Keg header dtor. This frees all data, destroys locks, frees the hash
1386 * table and removes the keg from the global list.
1388 * Arguments/Returns follow uma_dtor specifications
1392 keg_dtor(void *arg, int size, void *udata)
1396 keg = (uma_keg_t)arg;
1397 mtx_lock(&keg->uk_lock);
1398 if (keg->uk_free != 0) {
1399 printf("Freed UMA keg was not empty (%d items). "
1400 " Lost %d pages of memory.\n",
1401 keg->uk_free, keg->uk_pages);
1403 mtx_unlock(&keg->uk_lock);
1405 if (keg->uk_flags & UMA_ZONE_HASH)
1406 hash_free(&keg->uk_hash);
1408 mtx_destroy(&keg->uk_lock);
1414 * Arguments/Returns follow uma_dtor specifications
1418 zone_dtor(void *arg, int size, void *udata)
1423 zone = (uma_zone_t)arg;
1426 if (!(keg->uk_flags & UMA_ZFLAG_INTERNAL))
1431 if (keg->uk_flags & UMA_ZONE_SECONDARY) {
1432 LIST_REMOVE(zone, uz_link);
1434 * XXX there are some races here where
1435 * the zone can be drained but zone lock
1436 * released and then refilled before we
1437 * remove it... we dont care for now
1440 if (LIST_EMPTY(&keg->uk_zones))
1441 keg->uk_flags &= ~UMA_ZONE_SECONDARY;
1443 mtx_unlock(&uma_mtx);
1445 LIST_REMOVE(keg, uk_link);
1446 LIST_REMOVE(zone, uz_link);
1447 mtx_unlock(&uma_mtx);
1448 uma_zfree_internal(kegs, keg, NULL, SKIP_NONE);
1450 zone->uz_keg = NULL;
1454 * Traverses every zone in the system and calls a callback
1457 * zfunc A pointer to a function which accepts a zone
1464 zone_foreach(void (*zfunc)(uma_zone_t))
1470 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1471 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1474 mtx_unlock(&uma_mtx);
1477 /* Public functions */
1480 uma_startup(void *bootmem)
1482 struct uma_zctor_args args;
1485 u_int objsize, totsize, wsize;
1489 printf("Creating uma keg headers zone and keg.\n");
1492 * The general UMA lock is a recursion-allowed lock because
1493 * there is a code path where, while we're still configured
1494 * to use startup_alloc() for backend page allocations, we
1495 * may end up in uma_reclaim() which calls zone_foreach(zone_drain),
1496 * which grabs uma_mtx, only to later call into startup_alloc()
1497 * because while freeing we needed to allocate a bucket. Since
1498 * startup_alloc() also takes uma_mtx, we need to be able to
1501 mtx_init(&uma_mtx, "UMA lock", NULL, MTX_DEF | MTX_RECURSE);
1504 * Figure out the maximum number of items-per-slab we'll have if
1505 * we're using the OFFPAGE slab header to track free items, given
1506 * all possible object sizes and the maximum desired wastage
1509 * We iterate until we find an object size for
1510 * which the calculated wastage in zone_small_init() will be
1511 * enough to warrant OFFPAGE. Since wastedspace versus objsize
1512 * is an overall increasing see-saw function, we find the smallest
1513 * objsize such that the wastage is always acceptable for objects
1514 * with that objsize or smaller. Since a smaller objsize always
1515 * generates a larger possible uma_max_ipers, we use this computed
1516 * objsize to calculate the largest ipers possible. Since the
1517 * ipers calculated for OFFPAGE slab headers is always larger than
1518 * the ipers initially calculated in zone_small_init(), we use
1519 * the former's equation (UMA_SLAB_SIZE / keg->uk_rsize) to
1520 * obtain the maximum ipers possible for offpage slab headers.
1522 * It should be noted that ipers versus objsize is an inversly
1523 * proportional function which drops off rather quickly so as
1524 * long as our UMA_MAX_WASTE is such that the objsize we calculate
1525 * falls into the portion of the inverse relation AFTER the steep
1526 * falloff, then uma_max_ipers shouldn't be too high (~10 on i386).
1528 * Note that we have 8-bits (1 byte) to use as a freelist index
1529 * inside the actual slab header itself and this is enough to
1530 * accomodate us. In the worst case, a UMA_SMALLEST_UNIT sized
1531 * object with offpage slab header would have ipers =
1532 * UMA_SLAB_SIZE / UMA_SMALLEST_UNIT (currently = 256), which is
1533 * 1 greater than what our byte-integer freelist index can
1534 * accomodate, but we know that this situation never occurs as
1535 * for UMA_SMALLEST_UNIT-sized objects, we will never calculate
1536 * that we need to go to offpage slab headers. Or, if we do,
1537 * then we trap that condition below and panic in the INVARIANTS case.
1539 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab) - UMA_MAX_WASTE;
1541 objsize = UMA_SMALLEST_UNIT;
1542 while (totsize >= wsize) {
1543 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab)) /
1544 (objsize + UMA_FRITM_SZ);
1545 totsize *= (UMA_FRITM_SZ + objsize);
1548 if (objsize > UMA_SMALLEST_UNIT)
1550 uma_max_ipers = UMA_SLAB_SIZE / objsize;
1552 wsize = UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt) - UMA_MAX_WASTE;
1554 objsize = UMA_SMALLEST_UNIT;
1555 while (totsize >= wsize) {
1556 totsize = (UMA_SLAB_SIZE - sizeof(struct uma_slab_refcnt)) /
1557 (objsize + UMA_FRITMREF_SZ);
1558 totsize *= (UMA_FRITMREF_SZ + objsize);
1561 if (objsize > UMA_SMALLEST_UNIT)
1563 uma_max_ipers_ref = UMA_SLAB_SIZE / objsize;
1565 KASSERT((uma_max_ipers_ref <= 255) && (uma_max_ipers <= 255),
1566 ("uma_startup: calculated uma_max_ipers values too large!"));
1569 printf("Calculated uma_max_ipers (for OFFPAGE) is %d\n", uma_max_ipers);
1570 printf("Calculated uma_max_ipers_slab (for OFFPAGE) is %d\n",
1574 /* "manually" create the initial zone */
1575 args.name = "UMA Kegs";
1576 args.size = sizeof(struct uma_keg);
1577 args.ctor = keg_ctor;
1578 args.dtor = keg_dtor;
1579 args.uminit = zero_init;
1581 args.keg = &masterkeg;
1582 args.align = 32 - 1;
1583 args.flags = UMA_ZFLAG_INTERNAL;
1584 /* The initial zone has no Per cpu queues so it's smaller */
1585 zone_ctor(kegs, sizeof(struct uma_zone), &args, M_WAITOK);
1588 printf("Filling boot free list.\n");
1590 for (i = 0; i < UMA_BOOT_PAGES; i++) {
1591 slab = (uma_slab_t)((u_int8_t *)bootmem + (i * UMA_SLAB_SIZE));
1592 slab->us_data = (u_int8_t *)slab;
1593 slab->us_flags = UMA_SLAB_BOOT;
1594 LIST_INSERT_HEAD(&uma_boot_pages, slab, us_link);
1599 printf("Creating uma zone headers zone and keg.\n");
1601 args.name = "UMA Zones";
1602 args.size = sizeof(struct uma_zone) +
1603 (sizeof(struct uma_cache) * (mp_maxid + 1));
1604 args.ctor = zone_ctor;
1605 args.dtor = zone_dtor;
1606 args.uminit = zero_init;
1609 args.align = 32 - 1;
1610 args.flags = UMA_ZFLAG_INTERNAL;
1611 /* The initial zone has no Per cpu queues so it's smaller */
1612 zone_ctor(zones, sizeof(struct uma_zone), &args, M_WAITOK);
1615 printf("Initializing pcpu cache locks.\n");
1617 /* Initialize the pcpu cache lock set once and for all */
1618 for (i = 0; i <= mp_maxid; i++)
1622 printf("Creating slab and hash zones.\n");
1626 * This is the max number of free list items we'll have with
1629 slabsize = uma_max_ipers * UMA_FRITM_SZ;
1630 slabsize += sizeof(struct uma_slab);
1632 /* Now make a zone for slab headers */
1633 slabzone = uma_zcreate("UMA Slabs",
1635 NULL, NULL, NULL, NULL,
1636 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1639 * We also create a zone for the bigger slabs with reference
1640 * counts in them, to accomodate UMA_ZONE_REFCNT zones.
1642 slabsize = uma_max_ipers_ref * UMA_FRITMREF_SZ;
1643 slabsize += sizeof(struct uma_slab_refcnt);
1644 slabrefzone = uma_zcreate("UMA RCntSlabs",
1646 NULL, NULL, NULL, NULL,
1648 UMA_ZFLAG_INTERNAL);
1650 hashzone = uma_zcreate("UMA Hash",
1651 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
1652 NULL, NULL, NULL, NULL,
1653 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
1657 #ifdef UMA_MD_SMALL_ALLOC
1662 printf("UMA startup complete.\n");
1673 printf("UMA startup2 complete.\n");
1678 * Initialize our callout handle
1686 printf("Starting callout.\n");
1688 callout_init(&uma_callout, CALLOUT_MPSAFE);
1689 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
1691 printf("UMA startup3 complete.\n");
1696 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
1697 int align, u_int16_t flags)
1699 struct uma_kctor_args args;
1702 args.uminit = uminit;
1707 return (uma_zalloc_internal(kegs, &args, M_WAITOK));
1712 uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
1713 uma_init uminit, uma_fini fini, int align, u_int16_t flags)
1716 struct uma_zctor_args args;
1718 /* This stuff is essential for the zone ctor */
1723 args.uminit = uminit;
1729 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1734 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
1735 uma_init zinit, uma_fini zfini, uma_zone_t master)
1737 struct uma_zctor_args args;
1740 args.size = master->uz_keg->uk_size;
1743 args.uminit = zinit;
1745 args.align = master->uz_keg->uk_align;
1746 args.flags = master->uz_keg->uk_flags | UMA_ZONE_SECONDARY;
1747 args.keg = master->uz_keg;
1749 return (uma_zalloc_internal(zones, &args, M_WAITOK));
1754 uma_zdestroy(uma_zone_t zone)
1756 uma_zfree_internal(zones, zone, NULL, SKIP_NONE);
1761 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
1765 uma_bucket_t bucket;
1769 /* This is the fast path allocation */
1770 #ifdef UMA_DEBUG_ALLOC_1
1771 printf("Allocating one item from %s(%p)\n", zone->uz_name, zone);
1773 CTR3(KTR_UMA, "uma_zalloc_arg thread %x zone %s flags %d", curthread,
1774 zone->uz_name, flags);
1776 if (!(flags & M_NOWAIT)) {
1777 KASSERT(curthread->td_intr_nesting_level == 0,
1778 ("malloc(M_WAITOK) in interrupt context"));
1779 if (nosleepwithlocks) {
1781 badness = WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
1783 "malloc(M_WAITOK) of \"%s\", forcing M_NOWAIT",
1791 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
1792 "malloc(M_WAITOK) of \"%s\"", zone->uz_name);
1802 cpu = PCPU_GET(cpuid);
1804 cache = &zone->uz_cpu[cpu];
1807 bucket = cache->uc_allocbucket;
1810 if (bucket->ub_cnt > 0) {
1812 item = bucket->ub_bucket[bucket->ub_cnt];
1814 bucket->ub_bucket[bucket->ub_cnt] = NULL;
1816 KASSERT(item != NULL,
1817 ("uma_zalloc: Bucket pointer mangled."));
1821 uma_dbg_alloc(zone, NULL, item);
1825 if (zone->uz_ctor != NULL) {
1826 if (zone->uz_ctor(item, zone->uz_keg->uk_size,
1827 udata, flags) != 0) {
1828 uma_zfree_internal(zone, item, udata,
1834 bzero(item, zone->uz_keg->uk_size);
1836 } else if (cache->uc_freebucket) {
1838 * We have run out of items in our allocbucket.
1839 * See if we can switch with our free bucket.
1841 if (cache->uc_freebucket->ub_cnt > 0) {
1842 #ifdef UMA_DEBUG_ALLOC
1843 printf("uma_zalloc: Swapping empty with"
1846 bucket = cache->uc_freebucket;
1847 cache->uc_freebucket = cache->uc_allocbucket;
1848 cache->uc_allocbucket = bucket;
1855 /* Since we have locked the zone we may as well send back our stats */
1856 zone->uz_allocs += cache->uc_allocs;
1857 cache->uc_allocs = 0;
1859 /* Our old one is now a free bucket */
1860 if (cache->uc_allocbucket) {
1861 KASSERT(cache->uc_allocbucket->ub_cnt == 0,
1862 ("uma_zalloc_arg: Freeing a non free bucket."));
1863 LIST_INSERT_HEAD(&zone->uz_free_bucket,
1864 cache->uc_allocbucket, ub_link);
1865 cache->uc_allocbucket = NULL;
1868 /* Check the free list for a new alloc bucket */
1869 if ((bucket = LIST_FIRST(&zone->uz_full_bucket)) != NULL) {
1870 KASSERT(bucket->ub_cnt != 0,
1871 ("uma_zalloc_arg: Returning an empty bucket."));
1873 LIST_REMOVE(bucket, ub_link);
1874 cache->uc_allocbucket = bucket;
1878 /* We are no longer associated with this cpu!!! */
1881 /* Bump up our uz_count so we get here less */
1882 if (zone->uz_count < BUCKET_MAX)
1886 * Now lets just fill a bucket and put it on the free list. If that
1887 * works we'll restart the allocation from the begining.
1889 if (uma_zalloc_bucket(zone, flags)) {
1891 goto zalloc_restart;
1895 * We may not be able to get a bucket so return an actual item.
1898 printf("uma_zalloc_arg: Bucketzone returned NULL\n");
1901 return (uma_zalloc_internal(zone, udata, flags));
1905 uma_zone_slab(uma_zone_t zone, int flags)
1913 * This is to prevent us from recursively trying to allocate
1914 * buckets. The problem is that if an allocation forces us to
1915 * grab a new bucket we will call page_alloc, which will go off
1916 * and cause the vm to allocate vm_map_entries. If we need new
1917 * buckets there too we will recurse in kmem_alloc and bad
1918 * things happen. So instead we return a NULL bucket, and make
1919 * the code that allocates buckets smart enough to deal with it
1921 if (keg->uk_flags & UMA_ZFLAG_INTERNAL && keg->uk_recurse != 0)
1928 * Find a slab with some space. Prefer slabs that are partially
1929 * used over those that are totally full. This helps to reduce
1932 if (keg->uk_free != 0) {
1933 if (!LIST_EMPTY(&keg->uk_part_slab)) {
1934 slab = LIST_FIRST(&keg->uk_part_slab);
1936 slab = LIST_FIRST(&keg->uk_free_slab);
1937 LIST_REMOVE(slab, us_link);
1938 LIST_INSERT_HEAD(&keg->uk_part_slab, slab,
1945 * M_NOVM means don't ask at all!
1950 if (keg->uk_maxpages &&
1951 keg->uk_pages >= keg->uk_maxpages) {
1952 keg->uk_flags |= UMA_ZFLAG_FULL;
1954 if (flags & M_NOWAIT)
1957 msleep(keg, &keg->uk_lock, PVM,
1962 slab = slab_zalloc(zone, flags);
1966 * If we got a slab here it's safe to mark it partially used
1967 * and return. We assume that the caller is going to remove
1968 * at least one item.
1971 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
1975 * We might not have been able to get a slab but another cpu
1976 * could have while we were unlocked. Check again before we
1979 if (flags & M_NOWAIT)
1986 uma_slab_alloc(uma_zone_t zone, uma_slab_t slab)
1989 uma_slabrefcnt_t slabref;
1995 freei = slab->us_firstfree;
1996 if (keg->uk_flags & UMA_ZONE_REFCNT) {
1997 slabref = (uma_slabrefcnt_t)slab;
1998 slab->us_firstfree = slabref->us_freelist[freei].us_item;
2000 slab->us_firstfree = slab->us_freelist[freei].us_item;
2002 item = slab->us_data + (keg->uk_rsize * freei);
2004 slab->us_freecount--;
2007 uma_dbg_alloc(zone, slab, item);
2009 /* Move this slab to the full list */
2010 if (slab->us_freecount == 0) {
2011 LIST_REMOVE(slab, us_link);
2012 LIST_INSERT_HEAD(&keg->uk_full_slab, slab, us_link);
2019 uma_zalloc_bucket(uma_zone_t zone, int flags)
2021 uma_bucket_t bucket;
2024 int max, origflags = flags;
2027 * Try this zone's free list first so we don't allocate extra buckets.
2029 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2030 KASSERT(bucket->ub_cnt == 0,
2031 ("uma_zalloc_bucket: Bucket on free list is not empty."));
2032 LIST_REMOVE(bucket, ub_link);
2036 bflags = (flags & ~M_ZERO);
2037 if (zone->uz_keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2041 bucket = bucket_alloc(zone->uz_count, bflags);
2050 * This code is here to limit the number of simultaneous bucket fills
2051 * for any given zone to the number of per cpu caches in this zone. This
2052 * is done so that we don't allocate more memory than we really need.
2054 if (zone->uz_fills >= mp_ncpus)
2060 max = MIN(bucket->ub_entries, zone->uz_count);
2061 /* Try to keep the buckets totally full */
2062 saved = bucket->ub_cnt;
2063 while (bucket->ub_cnt < max &&
2064 (slab = uma_zone_slab(zone, flags)) != NULL) {
2065 while (slab->us_freecount && bucket->ub_cnt < max) {
2066 bucket->ub_bucket[bucket->ub_cnt++] =
2067 uma_slab_alloc(zone, slab);
2070 /* Don't block on the next fill */
2075 * We unlock here because we need to call the zone's init.
2076 * It should be safe to unlock because the slab dealt with
2077 * above is already on the appropriate list within the keg
2078 * and the bucket we filled is not yet on any list, so we
2081 if (zone->uz_init != NULL) {
2085 for (i = saved; i < bucket->ub_cnt; i++)
2086 if (zone->uz_init(bucket->ub_bucket[i],
2087 zone->uz_keg->uk_size, origflags) != 0)
2090 * If we couldn't initialize the whole bucket, put the
2091 * rest back onto the freelist.
2093 if (i != bucket->ub_cnt) {
2096 for (j = i; j < bucket->ub_cnt; j++)
2097 uma_zfree_internal(zone, bucket->ub_bucket[j],
2105 if (bucket->ub_cnt != 0) {
2106 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2113 bucket_free(bucket);
2118 * Allocates an item for an internal zone
2121 * zone The zone to alloc for.
2122 * udata The data to be passed to the constructor.
2123 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2126 * NULL if there is no memory and M_NOWAIT is set
2127 * An item if successful
2131 uma_zalloc_internal(uma_zone_t zone, void *udata, int flags)
2140 #ifdef UMA_DEBUG_ALLOC
2141 printf("INTERNAL: Allocating one item from %s(%p)\n", zone->uz_name, zone);
2145 slab = uma_zone_slab(zone, flags);
2151 item = uma_slab_alloc(zone, slab);
2156 * We have to call both the zone's init (not the keg's init)
2157 * and the zone's ctor. This is because the item is going from
2158 * a keg slab directly to the user, and the user is expecting it
2159 * to be both zone-init'd as well as zone-ctor'd.
2161 if (zone->uz_init != NULL) {
2162 if (zone->uz_init(item, keg->uk_size, flags) != 0) {
2163 uma_zfree_internal(zone, item, udata, SKIP_FINI);
2167 if (zone->uz_ctor != NULL) {
2168 if (zone->uz_ctor(item, keg->uk_size, udata, flags) != 0) {
2169 uma_zfree_internal(zone, item, udata, SKIP_DTOR);
2174 bzero(item, keg->uk_size);
2181 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
2185 uma_bucket_t bucket;
2188 enum zfreeskip skip;
2190 /* This is the fast path free */
2194 #ifdef UMA_DEBUG_ALLOC_1
2195 printf("Freeing item %p to %s(%p)\n", item, zone->uz_name, zone);
2197 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
2201 * The race here is acceptable. If we miss it we'll just have to wait
2202 * a little longer for the limits to be reset.
2205 if (keg->uk_flags & UMA_ZFLAG_FULL)
2206 goto zfree_internal;
2208 if (zone->uz_dtor) {
2209 zone->uz_dtor(item, keg->uk_size, udata);
2214 cpu = PCPU_GET(cpuid);
2216 cache = &zone->uz_cpu[cpu];
2219 bucket = cache->uc_freebucket;
2223 * Do we have room in our bucket? It is OK for this uz count
2224 * check to be slightly out of sync.
2227 if (bucket->ub_cnt < bucket->ub_entries) {
2228 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
2229 ("uma_zfree: Freeing to non free bucket index."));
2230 bucket->ub_bucket[bucket->ub_cnt] = item;
2234 if (keg->uk_flags & UMA_ZONE_MALLOC)
2235 uma_dbg_free(zone, udata, item);
2237 uma_dbg_free(zone, NULL, item);
2242 } else if (cache->uc_allocbucket) {
2243 #ifdef UMA_DEBUG_ALLOC
2244 printf("uma_zfree: Swapping buckets.\n");
2247 * We have run out of space in our freebucket.
2248 * See if we can switch with our alloc bucket.
2250 if (cache->uc_allocbucket->ub_cnt <
2251 cache->uc_freebucket->ub_cnt) {
2252 bucket = cache->uc_freebucket;
2253 cache->uc_freebucket = cache->uc_allocbucket;
2254 cache->uc_allocbucket = bucket;
2260 * We can get here for two reasons:
2262 * 1) The buckets are NULL
2263 * 2) The alloc and free buckets are both somewhat full.
2268 bucket = cache->uc_freebucket;
2269 cache->uc_freebucket = NULL;
2271 /* Can we throw this on the zone full list? */
2272 if (bucket != NULL) {
2273 #ifdef UMA_DEBUG_ALLOC
2274 printf("uma_zfree: Putting old bucket on the free list.\n");
2276 /* ub_cnt is pointing to the last free item */
2277 KASSERT(bucket->ub_cnt != 0,
2278 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
2279 LIST_INSERT_HEAD(&zone->uz_full_bucket,
2282 if ((bucket = LIST_FIRST(&zone->uz_free_bucket)) != NULL) {
2283 LIST_REMOVE(bucket, ub_link);
2285 cache->uc_freebucket = bucket;
2288 /* We're done with this CPU now */
2291 /* And the zone.. */
2294 #ifdef UMA_DEBUG_ALLOC
2295 printf("uma_zfree: Allocating new free bucket.\n");
2299 if (keg->uk_flags & UMA_ZFLAG_CACHEONLY)
2301 bucket = bucket_alloc(zone->uz_count, bflags);
2304 LIST_INSERT_HEAD(&zone->uz_free_bucket,
2311 * If nothing else caught this, we'll just do an internal free.
2318 * If we need to skip the dtor and the uma_dbg_free in
2319 * uma_zfree_internal because we've already called the dtor
2320 * above, but we ended up here, then we need to make sure
2321 * that we take care of the uma_dbg_free immediately.
2325 if (keg->uk_flags & UMA_ZONE_MALLOC)
2326 uma_dbg_free(zone, udata, item);
2328 uma_dbg_free(zone, NULL, item);
2332 uma_zfree_internal(zone, item, udata, skip);
2338 * Frees an item to an INTERNAL zone or allocates a free bucket
2341 * zone The zone to free to
2342 * item The item we're freeing
2343 * udata User supplied data for the dtor
2344 * skip Skip dtors and finis
2347 uma_zfree_internal(uma_zone_t zone, void *item, void *udata,
2348 enum zfreeskip skip)
2351 uma_slabrefcnt_t slabref;
2358 if (skip < SKIP_DTOR && zone->uz_dtor)
2359 zone->uz_dtor(item, keg->uk_size, udata);
2360 if (skip < SKIP_FINI && zone->uz_fini)
2361 zone->uz_fini(item, keg->uk_size);
2365 if (!(keg->uk_flags & UMA_ZONE_MALLOC)) {
2366 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
2367 if (keg->uk_flags & UMA_ZONE_HASH)
2368 slab = hash_sfind(&keg->uk_hash, mem);
2370 mem += keg->uk_pgoff;
2371 slab = (uma_slab_t)mem;
2374 slab = (uma_slab_t)udata;
2377 /* Do we need to remove from any lists? */
2378 if (slab->us_freecount+1 == keg->uk_ipers) {
2379 LIST_REMOVE(slab, us_link);
2380 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2381 } else if (slab->us_freecount == 0) {
2382 LIST_REMOVE(slab, us_link);
2383 LIST_INSERT_HEAD(&keg->uk_part_slab, slab, us_link);
2386 /* Slab management stuff */
2387 freei = ((unsigned long)item - (unsigned long)slab->us_data)
2392 uma_dbg_free(zone, slab, item);
2395 if (keg->uk_flags & UMA_ZONE_REFCNT) {
2396 slabref = (uma_slabrefcnt_t)slab;
2397 slabref->us_freelist[freei].us_item = slab->us_firstfree;
2399 slab->us_freelist[freei].us_item = slab->us_firstfree;
2401 slab->us_firstfree = freei;
2402 slab->us_freecount++;
2404 /* Zone statistics */
2407 if (keg->uk_flags & UMA_ZFLAG_FULL) {
2408 if (keg->uk_pages < keg->uk_maxpages)
2409 keg->uk_flags &= ~UMA_ZFLAG_FULL;
2411 /* We can handle one more allocation */
2420 uma_zone_set_max(uma_zone_t zone, int nitems)
2426 if (keg->uk_ppera > 1)
2427 keg->uk_maxpages = nitems * keg->uk_ppera;
2429 keg->uk_maxpages = nitems / keg->uk_ipers;
2431 if (keg->uk_maxpages * keg->uk_ipers < nitems)
2439 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
2442 KASSERT(zone->uz_keg->uk_pages == 0,
2443 ("uma_zone_set_init on non-empty keg"));
2444 zone->uz_keg->uk_init = uminit;
2450 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
2453 KASSERT(zone->uz_keg->uk_pages == 0,
2454 ("uma_zone_set_fini on non-empty keg"));
2455 zone->uz_keg->uk_fini = fini;
2461 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
2464 KASSERT(zone->uz_keg->uk_pages == 0,
2465 ("uma_zone_set_zinit on non-empty keg"));
2466 zone->uz_init = zinit;
2472 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
2475 KASSERT(zone->uz_keg->uk_pages == 0,
2476 ("uma_zone_set_zfini on non-empty keg"));
2477 zone->uz_fini = zfini;
2482 /* XXX uk_freef is not actually used with the zone locked */
2484 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
2487 zone->uz_keg->uk_freef = freef;
2492 /* XXX uk_allocf is not actually used with the zone locked */
2494 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
2497 zone->uz_keg->uk_flags |= UMA_ZFLAG_PRIVALLOC;
2498 zone->uz_keg->uk_allocf = allocf;
2504 uma_zone_set_obj(uma_zone_t zone, struct vm_object *obj, int count)
2511 pages = count / keg->uk_ipers;
2513 if (pages * keg->uk_ipers < count)
2516 kva = kmem_alloc_nofault(kernel_map, pages * UMA_SLAB_SIZE);
2521 obj = vm_object_allocate(OBJT_DEFAULT,
2524 VM_OBJECT_LOCK_INIT(obj, "uma object");
2525 _vm_object_allocate(OBJT_DEFAULT,
2531 keg->uk_maxpages = pages;
2532 keg->uk_allocf = obj_alloc;
2533 keg->uk_flags |= UMA_ZONE_NOFREE | UMA_ZFLAG_PRIVALLOC;
2540 uma_prealloc(uma_zone_t zone, int items)
2548 slabs = items / keg->uk_ipers;
2549 if (slabs * keg->uk_ipers < items)
2552 slab = slab_zalloc(zone, M_WAITOK);
2553 LIST_INSERT_HEAD(&keg->uk_free_slab, slab, us_link);
2561 uma_find_refcnt(uma_zone_t zone, void *item)
2563 uma_slabrefcnt_t slabref;
2569 slabref = (uma_slabrefcnt_t)vtoslab((vm_offset_t)item &
2571 KASSERT(slabref != NULL && slabref->us_keg->uk_flags & UMA_ZONE_REFCNT,
2572 ("uma_find_refcnt(): zone possibly not UMA_ZONE_REFCNT"));
2573 idx = ((unsigned long)item - (unsigned long)slabref->us_data)
2575 refcnt = &slabref->us_freelist[idx].us_refcnt;
2584 printf("UMA: vm asked us to release pages!\n");
2587 zone_foreach(zone_drain);
2589 * Some slabs may have been freed but this zone will be visited early
2590 * we visit again so that we can free pages that are empty once other
2591 * zones are drained. We have to do the same for buckets.
2593 zone_drain(slabzone);
2594 zone_drain(slabrefzone);
2595 bucket_zone_drain();
2599 uma_large_malloc(int size, int wait)
2605 slab = uma_zalloc_internal(slabzone, NULL, wait);
2608 mem = page_alloc(NULL, size, &flags, wait);
2610 vsetslab((vm_offset_t)mem, slab);
2611 slab->us_data = mem;
2612 slab->us_flags = flags | UMA_SLAB_MALLOC;
2613 slab->us_size = size;
2615 uma_zfree_internal(slabzone, slab, NULL, 0);
2622 uma_large_free(uma_slab_t slab)
2624 vsetobj((vm_offset_t)slab->us_data, kmem_object);
2625 page_free(slab->us_data, slab->us_size, slab->us_flags);
2626 uma_zfree_internal(slabzone, slab, NULL, 0);
2630 uma_print_stats(void)
2632 zone_foreach(uma_print_zone);
2636 slab_print(uma_slab_t slab)
2638 printf("slab: keg %p, data %p, freecount %d, firstfree %d\n",
2639 slab->us_keg, slab->us_data, slab->us_freecount,
2640 slab->us_firstfree);
2644 cache_print(uma_cache_t cache)
2646 printf("alloc: %p(%d), free: %p(%d)\n",
2647 cache->uc_allocbucket,
2648 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
2649 cache->uc_freebucket,
2650 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
2654 uma_print_zone(uma_zone_t zone)
2662 printf("%s(%p) size %d(%d) flags %d ipers %d ppera %d out %d free %d\n",
2663 zone->uz_name, zone, keg->uk_size, keg->uk_rsize, keg->uk_flags,
2664 keg->uk_ipers, keg->uk_ppera,
2665 (keg->uk_ipers * keg->uk_pages) - keg->uk_free, keg->uk_free);
2666 printf("Part slabs:\n");
2667 LIST_FOREACH(slab, &keg->uk_part_slab, us_link)
2669 printf("Free slabs:\n");
2670 LIST_FOREACH(slab, &keg->uk_free_slab, us_link)
2672 printf("Full slabs:\n");
2673 LIST_FOREACH(slab, &keg->uk_full_slab, us_link)
2675 for (i = 0; i <= mp_maxid; i++) {
2678 cache = &zone->uz_cpu[i];
2679 printf("CPU %d Cache:\n", i);
2685 * Sysctl handler for vm.zone
2687 * stolen from vm_zone.c
2690 sysctl_vm_zone(SYSCTL_HANDLER_ARGS)
2692 int error, len, cnt;
2693 const int linesize = 128; /* conservative */
2695 char *tmpbuf, *offset;
2701 uma_bucket_t bucket;
2706 LIST_FOREACH(zk, &uma_kegs, uk_link) {
2707 LIST_FOREACH(z, &zk->uk_zones, uz_link)
2710 mtx_unlock(&uma_mtx);
2711 MALLOC(tmpbuf, char *, (cnt == 0 ? 1 : cnt) * linesize,
2713 len = snprintf(tmpbuf, linesize,
2714 "\nITEM SIZE LIMIT USED FREE REQUESTS\n\n");
2716 tmpbuf[len - 1] = '\0';
2717 error = SYSCTL_OUT(req, tmpbuf, cnt == 0 ? len-1 : len);
2718 if (error || cnt == 0)
2722 LIST_FOREACH(zk, &uma_kegs, uk_link) {
2723 LIST_FOREACH(z, &zk->uk_zones, uz_link) {
2724 if (cnt == 0) /* list may have changed size */
2726 if (!(zk->uk_flags & UMA_ZFLAG_INTERNAL)) {
2727 for (cpu = 0; cpu <= mp_maxid; cpu++) {
2728 if (CPU_ABSENT(cpu))
2735 if (!(zk->uk_flags & UMA_ZFLAG_INTERNAL)) {
2736 for (cpu = 0; cpu <= mp_maxid; cpu++) {
2737 if (CPU_ABSENT(cpu))
2739 cache = &z->uz_cpu[cpu];
2740 if (cache->uc_allocbucket != NULL)
2741 cachefree += cache->uc_allocbucket->ub_cnt;
2742 if (cache->uc_freebucket != NULL)
2743 cachefree += cache->uc_freebucket->ub_cnt;
2747 LIST_FOREACH(bucket, &z->uz_full_bucket, ub_link) {
2748 cachefree += bucket->ub_cnt;
2750 totalfree = zk->uk_free + cachefree;
2751 len = snprintf(offset, linesize,
2752 "%-12.12s %6.6u, %8.8u, %6.6u, %6.6u, %8.8llu\n",
2753 z->uz_name, zk->uk_size,
2754 zk->uk_maxpages * zk->uk_ipers,
2755 (zk->uk_ipers * (zk->uk_pages / zk->uk_ppera)) - totalfree,
2757 (unsigned long long)z->uz_allocs);
2759 for (p = offset + 12; p > offset && *p == ' '; --p)
2766 mtx_unlock(&uma_mtx);
2768 error = SYSCTL_OUT(req, tmpbuf, offset - tmpbuf);
2770 FREE(tmpbuf, M_TEMP);