2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2005, 2009, 2013 Jeffrey Roberson <jeff@FreeBSD.org>
5 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
6 * Copyright (c) 2004-2006 Robert N. M. Watson
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * uma_core.c Implementation of the Universal Memory allocator
34 * This allocator is intended to replace the multitude of similar object caches
35 * in the standard FreeBSD kernel. The intent is to be flexible as well as
36 * efficient. A primary design goal is to return unused memory to the rest of
37 * the system. This will make the system as a whole more flexible due to the
38 * ability to move memory to subsystems which most need it instead of leaving
39 * pools of reserved memory unused.
41 * The basic ideas stem from similar slab/zone based allocators whose algorithms
48 * - Improve memory usage for large allocations
49 * - Investigate cache size adjustments
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
56 #include "opt_param.h"
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/bitset.h>
62 #include <sys/eventhandler.h>
63 #include <sys/kernel.h>
64 #include <sys/types.h>
65 #include <sys/limits.h>
66 #include <sys/queue.h>
67 #include <sys/malloc.h>
70 #include <sys/sysctl.h>
71 #include <sys/mutex.h>
73 #include <sys/random.h>
74 #include <sys/rwlock.h>
76 #include <sys/sched.h>
78 #include <sys/taskqueue.h>
79 #include <sys/vmmeter.h>
82 #include <vm/vm_object.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_pageout.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_phys.h>
87 #include <vm/vm_pagequeue.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
92 #include <vm/uma_int.h>
93 #include <vm/uma_dbg.h>
98 #include <vm/memguard.h>
102 * This is the zone and keg from which all zones are spawned.
104 static uma_zone_t kegs;
105 static uma_zone_t zones;
107 /* This is the zone from which all offpage uma_slab_ts are allocated. */
108 static uma_zone_t slabzone;
111 * The initial hash tables come out of this zone so they can be allocated
112 * prior to malloc coming up.
114 static uma_zone_t hashzone;
116 /* The boot-time adjusted value for cache line alignment. */
117 int uma_align_cache = 64 - 1;
119 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
122 * Are we allowed to allocate buckets?
124 static int bucketdisable = 1;
126 /* Linked list of all kegs in the system */
127 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
129 /* Linked list of all cache-only zones in the system */
130 static LIST_HEAD(,uma_zone) uma_cachezones =
131 LIST_HEAD_INITIALIZER(uma_cachezones);
133 /* This RW lock protects the keg list */
134 static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
137 * Pointer and counter to pool of pages, that is preallocated at
138 * startup to bootstrap UMA.
140 static char *bootmem;
141 static int boot_pages;
143 static struct sx uma_drain_lock;
145 /* kmem soft limit. */
146 static unsigned long uma_kmem_limit = LONG_MAX;
147 static volatile unsigned long uma_kmem_total;
149 /* Is the VM done starting up? */
150 static enum { BOOT_COLD = 0, BOOT_STRAPPED, BOOT_PAGEALLOC, BOOT_BUCKETS,
151 BOOT_RUNNING } booted = BOOT_COLD;
154 * This is the handle used to schedule events that need to happen
155 * outside of the allocation fast path.
157 static struct callout uma_callout;
158 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
161 * This structure is passed as the zone ctor arg so that I don't have to create
162 * a special allocation function just for zones.
164 struct uma_zctor_args {
179 struct uma_kctor_args {
188 struct uma_bucket_zone {
191 int ubz_entries; /* Number of items it can hold. */
192 int ubz_maxsize; /* Maximum allocation size per-item. */
196 * Compute the actual number of bucket entries to pack them in power
197 * of two sizes for more efficient space utilization.
199 #define BUCKET_SIZE(n) \
200 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
202 #define BUCKET_MAX BUCKET_SIZE(256)
204 struct uma_bucket_zone bucket_zones[] = {
205 { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
206 { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
207 { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
208 { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
209 { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
210 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
211 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
212 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
213 { NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
218 * Flags and enumerations to be passed to internal functions.
220 enum zfreeskip { SKIP_NONE = 0, SKIP_DTOR, SKIP_FINI };
222 #define UMA_ANYDOMAIN -1 /* Special value for domain search. */
226 int uma_startup_count(int);
227 void uma_startup(void *, int);
228 void uma_startup1(void);
229 void uma_startup2(void);
231 static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
232 static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
233 static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
234 static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
235 static void page_free(void *, vm_size_t, uint8_t);
236 static void pcpu_page_free(void *, vm_size_t, uint8_t);
237 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int);
238 static void cache_drain(uma_zone_t);
239 static void bucket_drain(uma_zone_t, uma_bucket_t);
240 static void bucket_cache_drain(uma_zone_t zone);
241 static int keg_ctor(void *, int, void *, int);
242 static void keg_dtor(void *, int, void *);
243 static int zone_ctor(void *, int, void *, int);
244 static void zone_dtor(void *, int, void *);
245 static int zero_init(void *, int, int);
246 static void keg_small_init(uma_keg_t keg);
247 static void keg_large_init(uma_keg_t keg);
248 static void zone_foreach(void (*zfunc)(uma_zone_t));
249 static void zone_timeout(uma_zone_t zone);
250 static int hash_alloc(struct uma_hash *);
251 static int hash_expand(struct uma_hash *, struct uma_hash *);
252 static void hash_free(struct uma_hash *hash);
253 static void uma_timeout(void *);
254 static void uma_startup3(void);
255 static void *zone_alloc_item(uma_zone_t, void *, int, int);
256 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
257 static void bucket_enable(void);
258 static void bucket_init(void);
259 static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
260 static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
261 static void bucket_zone_drain(void);
262 static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int);
263 static uma_slab_t zone_fetch_slab(uma_zone_t, uma_keg_t, int, int);
264 static uma_slab_t zone_fetch_slab_multi(uma_zone_t, uma_keg_t, int, int);
265 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
266 static void slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item);
267 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
268 uma_fini fini, int align, uint32_t flags);
269 static int zone_import(uma_zone_t, void **, int, int, int);
270 static void zone_release(uma_zone_t, void **, int);
271 static void uma_zero_item(void *, uma_zone_t);
273 void uma_print_zone(uma_zone_t);
274 void uma_print_stats(void);
275 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
276 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
279 static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
280 static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
281 static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
282 static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
284 static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0,
285 "Memory allocation debugging");
287 static u_int dbg_divisor = 1;
288 SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
289 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
290 "Debug & thrash every this item in memory allocator");
292 static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
293 static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
294 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
295 &uma_dbg_cnt, "memory items debugged");
296 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
297 &uma_skip_cnt, "memory items skipped, not debugged");
300 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
302 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
303 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
305 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
306 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
308 static int zone_warnings = 1;
309 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
310 "Warn when UMA zones becomes full");
312 /* Adjust bytes under management by UMA. */
314 uma_total_dec(unsigned long size)
317 atomic_subtract_long(&uma_kmem_total, size);
321 uma_total_inc(unsigned long size)
324 if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
325 uma_reclaim_wakeup();
329 * This routine checks to see whether or not it's safe to enable buckets.
334 bucketdisable = vm_page_count_min();
338 * Initialize bucket_zones, the array of zones of buckets of various sizes.
340 * For each zone, calculate the memory required for each bucket, consisting
341 * of the header and an array of pointers.
346 struct uma_bucket_zone *ubz;
349 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
350 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
351 size += sizeof(void *) * ubz->ubz_entries;
352 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
353 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
354 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | UMA_ZONE_NUMA);
359 * Given a desired number of entries for a bucket, return the zone from which
360 * to allocate the bucket.
362 static struct uma_bucket_zone *
363 bucket_zone_lookup(int entries)
365 struct uma_bucket_zone *ubz;
367 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
368 if (ubz->ubz_entries >= entries)
375 bucket_select(int size)
377 struct uma_bucket_zone *ubz;
379 ubz = &bucket_zones[0];
380 if (size > ubz->ubz_maxsize)
381 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
383 for (; ubz->ubz_entries != 0; ubz++)
384 if (ubz->ubz_maxsize < size)
387 return (ubz->ubz_entries);
391 bucket_alloc(uma_zone_t zone, void *udata, int flags)
393 struct uma_bucket_zone *ubz;
397 * This is to stop us from allocating per cpu buckets while we're
398 * running out of vm.boot_pages. Otherwise, we would exhaust the
399 * boot pages. This also prevents us from allocating buckets in
400 * low memory situations.
405 * To limit bucket recursion we store the original zone flags
406 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
407 * NOVM flag to persist even through deep recursions. We also
408 * store ZFLAG_BUCKET once we have recursed attempting to allocate
409 * a bucket for a bucket zone so we do not allow infinite bucket
410 * recursion. This cookie will even persist to frees of unused
411 * buckets via the allocation path or bucket allocations in the
414 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
415 udata = (void *)(uintptr_t)zone->uz_flags;
417 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
419 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
421 if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
423 ubz = bucket_zone_lookup(zone->uz_count);
424 if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
426 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
429 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
432 bucket->ub_entries = ubz->ubz_entries;
439 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
441 struct uma_bucket_zone *ubz;
443 KASSERT(bucket->ub_cnt == 0,
444 ("bucket_free: Freeing a non free bucket."));
445 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
446 udata = (void *)(uintptr_t)zone->uz_flags;
447 ubz = bucket_zone_lookup(bucket->ub_entries);
448 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
452 bucket_zone_drain(void)
454 struct uma_bucket_zone *ubz;
456 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
457 zone_drain(ubz->ubz_zone);
461 zone_log_warning(uma_zone_t zone)
463 static const struct timeval warninterval = { 300, 0 };
465 if (!zone_warnings || zone->uz_warning == NULL)
468 if (ratecheck(&zone->uz_ratecheck, &warninterval))
469 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
473 zone_maxaction(uma_zone_t zone)
476 if (zone->uz_maxaction.ta_func != NULL)
477 taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
481 zone_foreach_keg(uma_zone_t zone, void (*kegfn)(uma_keg_t))
485 LIST_FOREACH(klink, &zone->uz_kegs, kl_link)
486 kegfn(klink->kl_keg);
490 * Routine called by timeout which is used to fire off some time interval
491 * based calculations. (stats, hash size, etc.)
500 uma_timeout(void *unused)
503 zone_foreach(zone_timeout);
505 /* Reschedule this event */
506 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
510 * Routine to perform timeout driven calculations. This expands the
511 * hashes and does per cpu statistics aggregation.
516 keg_timeout(uma_keg_t keg)
521 * Expand the keg hash table.
523 * This is done if the number of slabs is larger than the hash size.
524 * What I'm trying to do here is completely reduce collisions. This
525 * may be a little aggressive. Should I allow for two collisions max?
527 if (keg->uk_flags & UMA_ZONE_HASH &&
528 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
529 struct uma_hash newhash;
530 struct uma_hash oldhash;
534 * This is so involved because allocating and freeing
535 * while the keg lock is held will lead to deadlock.
536 * I have to do everything in stages and check for
539 newhash = keg->uk_hash;
541 ret = hash_alloc(&newhash);
544 if (hash_expand(&keg->uk_hash, &newhash)) {
545 oldhash = keg->uk_hash;
546 keg->uk_hash = newhash;
559 zone_timeout(uma_zone_t zone)
562 zone_foreach_keg(zone, &keg_timeout);
566 * Allocate and zero fill the next sized hash table from the appropriate
570 * hash A new hash structure with the old hash size in uh_hashsize
573 * 1 on success and 0 on failure.
576 hash_alloc(struct uma_hash *hash)
581 oldsize = hash->uh_hashsize;
583 /* We're just going to go to a power of two greater */
585 hash->uh_hashsize = oldsize * 2;
586 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
587 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
588 M_UMAHASH, M_NOWAIT);
590 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
591 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
592 UMA_ANYDOMAIN, M_WAITOK);
593 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
595 if (hash->uh_slab_hash) {
596 bzero(hash->uh_slab_hash, alloc);
597 hash->uh_hashmask = hash->uh_hashsize - 1;
605 * Expands the hash table for HASH zones. This is done from zone_timeout
606 * to reduce collisions. This must not be done in the regular allocation
607 * path, otherwise, we can recurse on the vm while allocating pages.
610 * oldhash The hash you want to expand
611 * newhash The hash structure for the new table
619 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
625 if (!newhash->uh_slab_hash)
628 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
632 * I need to investigate hash algorithms for resizing without a
636 for (i = 0; i < oldhash->uh_hashsize; i++)
637 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
638 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
639 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
640 hval = UMA_HASH(newhash, slab->us_data);
641 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
649 * Free the hash bucket to the appropriate backing store.
652 * slab_hash The hash bucket we're freeing
653 * hashsize The number of entries in that hash bucket
659 hash_free(struct uma_hash *hash)
661 if (hash->uh_slab_hash == NULL)
663 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
664 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
666 free(hash->uh_slab_hash, M_UMAHASH);
670 * Frees all outstanding items in a bucket
673 * zone The zone to free to, must be unlocked.
674 * bucket The free/alloc bucket with items, cpu queue must be locked.
681 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
689 for (i = 0; i < bucket->ub_cnt; i++)
690 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
691 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
696 * Drains the per cpu caches for a zone.
698 * NOTE: This may only be called while the zone is being turn down, and not
699 * during normal operation. This is necessary in order that we do not have
700 * to migrate CPUs to drain the per-CPU caches.
703 * zone The zone to drain, must be unlocked.
709 cache_drain(uma_zone_t zone)
715 * XXX: It is safe to not lock the per-CPU caches, because we're
716 * tearing down the zone anyway. I.e., there will be no further use
717 * of the caches at this point.
719 * XXX: It would good to be able to assert that the zone is being
720 * torn down to prevent improper use of cache_drain().
722 * XXX: We lock the zone before passing into bucket_cache_drain() as
723 * it is used elsewhere. Should the tear-down path be made special
724 * there in some form?
727 cache = &zone->uz_cpu[cpu];
728 bucket_drain(zone, cache->uc_allocbucket);
729 bucket_drain(zone, cache->uc_freebucket);
730 if (cache->uc_allocbucket != NULL)
731 bucket_free(zone, cache->uc_allocbucket, NULL);
732 if (cache->uc_freebucket != NULL)
733 bucket_free(zone, cache->uc_freebucket, NULL);
734 cache->uc_allocbucket = cache->uc_freebucket = NULL;
737 bucket_cache_drain(zone);
742 cache_shrink(uma_zone_t zone)
745 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
749 zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
754 cache_drain_safe_cpu(uma_zone_t zone)
760 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
766 if (zone->uz_flags & UMA_ZONE_NUMA)
767 domain = PCPU_GET(domain);
770 cache = &zone->uz_cpu[curcpu];
771 if (cache->uc_allocbucket) {
772 if (cache->uc_allocbucket->ub_cnt != 0)
773 LIST_INSERT_HEAD(&zone->uz_domain[domain].uzd_buckets,
774 cache->uc_allocbucket, ub_link);
776 b1 = cache->uc_allocbucket;
777 cache->uc_allocbucket = NULL;
779 if (cache->uc_freebucket) {
780 if (cache->uc_freebucket->ub_cnt != 0)
781 LIST_INSERT_HEAD(&zone->uz_domain[domain].uzd_buckets,
782 cache->uc_freebucket, ub_link);
784 b2 = cache->uc_freebucket;
785 cache->uc_freebucket = NULL;
790 bucket_free(zone, b1, NULL);
792 bucket_free(zone, b2, NULL);
796 * Safely drain per-CPU caches of a zone(s) to alloc bucket.
797 * This is an expensive call because it needs to bind to all CPUs
798 * one by one and enter a critical section on each of them in order
799 * to safely access their cache buckets.
800 * Zone lock must not be held on call this function.
803 cache_drain_safe(uma_zone_t zone)
808 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
813 zone_foreach(cache_shrink);
816 thread_lock(curthread);
817 sched_bind(curthread, cpu);
818 thread_unlock(curthread);
821 cache_drain_safe_cpu(zone);
823 zone_foreach(cache_drain_safe_cpu);
825 thread_lock(curthread);
826 sched_unbind(curthread);
827 thread_unlock(curthread);
831 * Drain the cached buckets from a zone. Expects a locked zone on entry.
834 bucket_cache_drain(uma_zone_t zone)
836 uma_zone_domain_t zdom;
841 * Drain the bucket queues and free the buckets.
843 for (i = 0; i < vm_ndomains; i++) {
844 zdom = &zone->uz_domain[i];
845 while ((bucket = LIST_FIRST(&zdom->uzd_buckets)) != NULL) {
846 LIST_REMOVE(bucket, ub_link);
848 bucket_drain(zone, bucket);
849 bucket_free(zone, bucket, NULL);
855 * Shrink further bucket sizes. Price of single zone lock collision
856 * is probably lower then price of global cache drain.
858 if (zone->uz_count > zone->uz_count_min)
863 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
869 CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
870 keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
873 flags = slab->us_flags;
875 if (keg->uk_fini != NULL) {
876 for (i--; i > -1; i--)
879 * trash_fini implies that dtor was trash_dtor. trash_fini
880 * would check that memory hasn't been modified since free,
881 * which executed trash_dtor.
882 * That's why we need to run uma_dbg_kskip() check here,
883 * albeit we don't make skip check for other init/fini
886 if (!uma_dbg_kskip(keg, slab->us_data + (keg->uk_rsize * i)) ||
887 keg->uk_fini != trash_fini)
889 keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
892 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
893 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
894 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
895 uma_total_dec(PAGE_SIZE * keg->uk_ppera);
899 * Frees pages from a keg back to the system. This is done on demand from
900 * the pageout daemon.
905 keg_drain(uma_keg_t keg)
907 struct slabhead freeslabs = { 0 };
909 uma_slab_t slab, tmp;
913 * We don't want to take pages from statically allocated kegs at this
916 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
919 CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u",
920 keg->uk_name, keg, keg->uk_free);
922 if (keg->uk_free == 0)
925 for (i = 0; i < vm_ndomains; i++) {
926 dom = &keg->uk_domain[i];
927 LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) {
928 /* We have nowhere to free these to. */
929 if (slab->us_flags & UMA_SLAB_BOOT)
932 LIST_REMOVE(slab, us_link);
933 keg->uk_pages -= keg->uk_ppera;
934 keg->uk_free -= keg->uk_ipers;
936 if (keg->uk_flags & UMA_ZONE_HASH)
937 UMA_HASH_REMOVE(&keg->uk_hash, slab,
940 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
947 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
948 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
949 keg_free_slab(keg, slab, keg->uk_ipers);
954 zone_drain_wait(uma_zone_t zone, int waitok)
958 * Set draining to interlock with zone_dtor() so we can release our
959 * locks as we go. Only dtor() should do a WAITOK call since it
960 * is the only call that knows the structure will still be available
964 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
965 if (waitok == M_NOWAIT)
967 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
969 zone->uz_flags |= UMA_ZFLAG_DRAINING;
970 bucket_cache_drain(zone);
973 * The DRAINING flag protects us from being freed while
974 * we're running. Normally the uma_rwlock would protect us but we
975 * must be able to release and acquire the right lock for each keg.
977 zone_foreach_keg(zone, &keg_drain);
979 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
986 zone_drain(uma_zone_t zone)
989 zone_drain_wait(zone, M_NOWAIT);
993 * Allocate a new slab for a keg. This does not insert the slab onto a list.
996 * wait Shall we wait?
999 * The slab that was allocated or NULL if there is no memory and the
1000 * caller specified M_NOWAIT.
1003 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int wait)
1012 KASSERT(domain >= 0 && domain < vm_ndomains,
1013 ("keg_alloc_slab: domain %d out of range", domain));
1014 mtx_assert(&keg->uk_lock, MA_OWNED);
1018 allocf = keg->uk_allocf;
1020 size = keg->uk_ppera * PAGE_SIZE;
1022 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1023 slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, wait);
1029 * This reproduces the old vm_zone behavior of zero filling pages the
1030 * first time they are added to a zone.
1032 * Malloced items are zeroed in uma_zalloc.
1035 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1040 if (keg->uk_flags & UMA_ZONE_NODUMP)
1043 /* zone is passed for legacy reasons. */
1044 mem = allocf(zone, size, domain, &flags, wait);
1046 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1047 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
1051 uma_total_inc(size);
1053 /* Point the slab into the allocated memory */
1054 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
1055 slab = (uma_slab_t )(mem + keg->uk_pgoff);
1057 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
1058 for (i = 0; i < keg->uk_ppera; i++)
1059 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
1062 slab->us_data = mem;
1063 slab->us_freecount = keg->uk_ipers;
1064 slab->us_flags = flags;
1065 slab->us_domain = domain;
1066 BIT_FILL(SLAB_SETSIZE, &slab->us_free);
1068 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1071 if (keg->uk_init != NULL) {
1072 for (i = 0; i < keg->uk_ipers; i++)
1073 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1074 keg->uk_size, wait) != 0)
1076 if (i != keg->uk_ipers) {
1077 keg_free_slab(keg, slab, i);
1085 CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
1086 slab, keg->uk_name, keg);
1089 if (keg->uk_flags & UMA_ZONE_HASH)
1090 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
1092 keg->uk_pages += keg->uk_ppera;
1093 keg->uk_free += keg->uk_ipers;
1100 * This function is intended to be used early on in place of page_alloc() so
1101 * that we may use the boot time page cache to satisfy allocations before
1105 startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1112 keg = zone_first_keg(zone);
1115 * If we are in BOOT_BUCKETS or higher, than switch to real
1116 * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC.
1122 case BOOT_PAGEALLOC:
1123 if (keg->uk_ppera > 1)
1127 #ifdef UMA_MD_SMALL_ALLOC
1128 keg->uk_allocf = (keg->uk_ppera > 1) ?
1129 page_alloc : uma_small_alloc;
1131 keg->uk_allocf = page_alloc;
1133 return keg->uk_allocf(zone, bytes, domain, pflag, wait);
1137 * Check our small startup cache to see if it has pages remaining.
1139 pages = howmany(bytes, PAGE_SIZE);
1140 KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1141 if (pages > boot_pages)
1142 panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name);
1144 printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name,
1148 boot_pages -= pages;
1149 bootmem += pages * PAGE_SIZE;
1150 *pflag = UMA_SLAB_BOOT;
1156 * Allocates a number of pages from the system
1159 * bytes The number of bytes requested
1160 * wait Shall we wait?
1163 * A pointer to the alloced memory or possibly
1164 * NULL if M_NOWAIT is set.
1167 page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1170 void *p; /* Returned page */
1172 *pflag = UMA_SLAB_KERNEL;
1173 p = (void *) kmem_malloc_domain(domain, bytes, wait);
1179 pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1182 struct pglist alloctail;
1183 vm_offset_t addr, zkva;
1185 vm_page_t p, p_next;
1190 MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1192 TAILQ_INIT(&alloctail);
1193 flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1194 malloc2vm_flags(wait);
1195 *pflag = UMA_SLAB_KERNEL;
1196 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1197 if (CPU_ABSENT(cpu)) {
1198 p = vm_page_alloc(NULL, 0, flags);
1201 p = vm_page_alloc(NULL, 0, flags);
1203 pc = pcpu_find(cpu);
1204 p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags);
1205 if (__predict_false(p == NULL))
1206 p = vm_page_alloc(NULL, 0, flags);
1209 if (__predict_false(p == NULL))
1211 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1213 if ((addr = kva_alloc(bytes)) == 0)
1216 TAILQ_FOREACH(p, &alloctail, listq) {
1217 pmap_qenter(zkva, &p, 1);
1220 return ((void*)addr);
1222 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1223 vm_page_unwire(p, PQ_NONE);
1230 * Allocates a number of pages from within an object
1233 * bytes The number of bytes requested
1234 * wait Shall we wait?
1237 * A pointer to the alloced memory or possibly
1238 * NULL if M_NOWAIT is set.
1241 noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1244 TAILQ_HEAD(, vm_page) alloctail;
1246 vm_offset_t retkva, zkva;
1247 vm_page_t p, p_next;
1250 TAILQ_INIT(&alloctail);
1251 keg = zone_first_keg(zone);
1253 npages = howmany(bytes, PAGE_SIZE);
1254 while (npages > 0) {
1255 p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
1256 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1257 ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1261 * Since the page does not belong to an object, its
1264 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1269 * Page allocation failed, free intermediate pages and
1272 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1273 vm_page_unwire(p, PQ_NONE);
1278 *flags = UMA_SLAB_PRIV;
1279 zkva = keg->uk_kva +
1280 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1282 TAILQ_FOREACH(p, &alloctail, listq) {
1283 pmap_qenter(zkva, &p, 1);
1287 return ((void *)retkva);
1291 * Frees a number of pages to the system
1294 * mem A pointer to the memory to be freed
1295 * size The size of the memory being freed
1296 * flags The original p->us_flags field
1302 page_free(void *mem, vm_size_t size, uint8_t flags)
1305 if ((flags & UMA_SLAB_KERNEL) == 0)
1306 panic("UMA: page_free used with invalid flags %x", flags);
1308 kmem_free((vm_offset_t)mem, size);
1312 * Frees pcpu zone allocations
1315 * mem A pointer to the memory to be freed
1316 * size The size of the memory being freed
1317 * flags The original p->us_flags field
1323 pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1325 vm_offset_t sva, curva;
1329 MPASS(size == (mp_maxid+1)*PAGE_SIZE);
1330 sva = (vm_offset_t)mem;
1331 for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1332 paddr = pmap_kextract(curva);
1333 m = PHYS_TO_VM_PAGE(paddr);
1334 vm_page_unwire(m, PQ_NONE);
1337 pmap_qremove(sva, size >> PAGE_SHIFT);
1338 kva_free(sva, size);
1343 * Zero fill initializer
1345 * Arguments/Returns follow uma_init specifications
1348 zero_init(void *mem, int size, int flags)
1355 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1358 * keg The zone we should initialize
1364 keg_small_init(uma_keg_t keg)
1372 if (keg->uk_flags & UMA_ZONE_PCPU) {
1373 u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU;
1375 slabsize = UMA_PCPU_ALLOC_SIZE;
1376 keg->uk_ppera = ncpus;
1378 slabsize = UMA_SLAB_SIZE;
1383 * Calculate the size of each allocation (rsize) according to
1384 * alignment. If the requested size is smaller than we have
1385 * allocation bits for we round it up.
1387 rsize = keg->uk_size;
1388 if (rsize < slabsize / SLAB_SETSIZE)
1389 rsize = slabsize / SLAB_SETSIZE;
1390 if (rsize & keg->uk_align)
1391 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1392 keg->uk_rsize = rsize;
1394 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1395 keg->uk_rsize < UMA_PCPU_ALLOC_SIZE,
1396 ("%s: size %u too large", __func__, keg->uk_rsize));
1398 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1401 shsize = sizeof(struct uma_slab);
1403 if (rsize <= slabsize - shsize)
1404 keg->uk_ipers = (slabsize - shsize) / rsize;
1406 /* Handle special case when we have 1 item per slab, so
1407 * alignment requirement can be relaxed. */
1408 KASSERT(keg->uk_size <= slabsize - shsize,
1409 ("%s: size %u greater than slab", __func__, keg->uk_size));
1412 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1413 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1415 memused = keg->uk_ipers * rsize + shsize;
1416 wastedspace = slabsize - memused;
1419 * We can't do OFFPAGE if we're internal or if we've been
1420 * asked to not go to the VM for buckets. If we do this we
1421 * may end up going to the VM for slabs which we do not
1422 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
1423 * of UMA_ZONE_VM, which clearly forbids it.
1425 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1426 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1430 * See if using an OFFPAGE slab will limit our waste. Only do
1431 * this if it permits more items per-slab.
1433 * XXX We could try growing slabsize to limit max waste as well.
1434 * Historically this was not done because the VM could not
1435 * efficiently handle contiguous allocations.
1437 if ((wastedspace >= slabsize / UMA_MAX_WASTE) &&
1438 (keg->uk_ipers < (slabsize / keg->uk_rsize))) {
1439 keg->uk_ipers = slabsize / keg->uk_rsize;
1440 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1441 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1442 CTR6(KTR_UMA, "UMA decided we need offpage slab headers for "
1443 "keg: %s(%p), calculated wastedspace = %d, "
1444 "maximum wasted space allowed = %d, "
1445 "calculated ipers = %d, "
1446 "new wasted space = %d\n", keg->uk_name, keg, wastedspace,
1447 slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1448 slabsize - keg->uk_ipers * keg->uk_rsize);
1449 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1452 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1453 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1454 keg->uk_flags |= UMA_ZONE_HASH;
1458 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1459 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1463 * keg The keg we should initialize
1469 keg_large_init(uma_keg_t keg)
1473 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1474 KASSERT((keg->uk_flags & UMA_ZFLAG_CACHEONLY) == 0,
1475 ("keg_large_init: Cannot large-init a UMA_ZFLAG_CACHEONLY keg"));
1476 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1477 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1479 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1481 keg->uk_rsize = keg->uk_size;
1483 /* Check whether we have enough space to not do OFFPAGE. */
1484 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0) {
1485 shsize = sizeof(struct uma_slab);
1486 if (shsize & UMA_ALIGN_PTR)
1487 shsize = (shsize & ~UMA_ALIGN_PTR) +
1488 (UMA_ALIGN_PTR + 1);
1490 if (PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < shsize) {
1492 * We can't do OFFPAGE if we're internal, in which case
1493 * we need an extra page per allocation to contain the
1496 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0)
1497 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1503 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1504 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1505 keg->uk_flags |= UMA_ZONE_HASH;
1509 keg_cachespread_init(uma_keg_t keg)
1516 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1517 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1519 alignsize = keg->uk_align + 1;
1520 rsize = keg->uk_size;
1522 * We want one item to start on every align boundary in a page. To
1523 * do this we will span pages. We will also extend the item by the
1524 * size of align if it is an even multiple of align. Otherwise, it
1525 * would fall on the same boundary every time.
1527 if (rsize & keg->uk_align)
1528 rsize = (rsize & ~keg->uk_align) + alignsize;
1529 if ((rsize & alignsize) == 0)
1531 trailer = rsize - keg->uk_size;
1532 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1533 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1534 keg->uk_rsize = rsize;
1535 keg->uk_ppera = pages;
1536 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1537 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1538 KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
1539 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1544 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1545 * the keg onto the global keg list.
1547 * Arguments/Returns follow uma_ctor specifications
1548 * udata Actually uma_kctor_args
1551 keg_ctor(void *mem, int size, void *udata, int flags)
1553 struct uma_kctor_args *arg = udata;
1554 uma_keg_t keg = mem;
1558 keg->uk_size = arg->size;
1559 keg->uk_init = arg->uminit;
1560 keg->uk_fini = arg->fini;
1561 keg->uk_align = arg->align;
1564 keg->uk_reserve = 0;
1566 keg->uk_flags = arg->flags;
1567 keg->uk_slabzone = NULL;
1570 * The master zone is passed to us at keg-creation time.
1573 keg->uk_name = zone->uz_name;
1575 if (arg->flags & UMA_ZONE_VM)
1576 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1578 if (arg->flags & UMA_ZONE_ZINIT)
1579 keg->uk_init = zero_init;
1581 if (arg->flags & UMA_ZONE_MALLOC)
1582 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1584 if (arg->flags & UMA_ZONE_PCPU)
1586 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1588 keg->uk_flags &= ~UMA_ZONE_PCPU;
1591 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1592 keg_cachespread_init(keg);
1594 if (keg->uk_size > UMA_SLAB_SPACE)
1595 keg_large_init(keg);
1597 keg_small_init(keg);
1600 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1601 keg->uk_slabzone = slabzone;
1604 * If we haven't booted yet we need allocations to go through the
1605 * startup cache until the vm is ready.
1607 if (booted < BOOT_PAGEALLOC)
1608 keg->uk_allocf = startup_alloc;
1609 #ifdef UMA_MD_SMALL_ALLOC
1610 else if (keg->uk_ppera == 1)
1611 keg->uk_allocf = uma_small_alloc;
1613 else if (keg->uk_flags & UMA_ZONE_PCPU)
1614 keg->uk_allocf = pcpu_page_alloc;
1616 keg->uk_allocf = page_alloc;
1617 #ifdef UMA_MD_SMALL_ALLOC
1618 if (keg->uk_ppera == 1)
1619 keg->uk_freef = uma_small_free;
1622 if (keg->uk_flags & UMA_ZONE_PCPU)
1623 keg->uk_freef = pcpu_page_free;
1625 keg->uk_freef = page_free;
1628 * Initialize keg's lock
1630 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1633 * If we're putting the slab header in the actual page we need to
1634 * figure out where in each page it goes. This calculates a right
1635 * justified offset into the memory on an ALIGN_PTR boundary.
1637 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1640 /* Size of the slab struct and free list */
1641 totsize = sizeof(struct uma_slab);
1643 if (totsize & UMA_ALIGN_PTR)
1644 totsize = (totsize & ~UMA_ALIGN_PTR) +
1645 (UMA_ALIGN_PTR + 1);
1646 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - totsize;
1649 * The only way the following is possible is if with our
1650 * UMA_ALIGN_PTR adjustments we are now bigger than
1651 * UMA_SLAB_SIZE. I haven't checked whether this is
1652 * mathematically possible for all cases, so we make
1655 totsize = keg->uk_pgoff + sizeof(struct uma_slab);
1656 if (totsize > PAGE_SIZE * keg->uk_ppera) {
1657 printf("zone %s ipers %d rsize %d size %d\n",
1658 zone->uz_name, keg->uk_ipers, keg->uk_rsize,
1660 panic("UMA slab won't fit.");
1664 if (keg->uk_flags & UMA_ZONE_HASH)
1665 hash_alloc(&keg->uk_hash);
1667 CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n",
1668 keg, zone->uz_name, zone,
1669 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
1672 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1674 rw_wlock(&uma_rwlock);
1675 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1676 rw_wunlock(&uma_rwlock);
1681 * Zone header ctor. This initializes all fields, locks, etc.
1683 * Arguments/Returns follow uma_ctor specifications
1684 * udata Actually uma_zctor_args
1687 zone_ctor(void *mem, int size, void *udata, int flags)
1689 struct uma_zctor_args *arg = udata;
1690 uma_zone_t zone = mem;
1695 zone->uz_name = arg->name;
1696 zone->uz_ctor = arg->ctor;
1697 zone->uz_dtor = arg->dtor;
1698 zone->uz_slab = zone_fetch_slab;
1699 zone->uz_init = NULL;
1700 zone->uz_fini = NULL;
1701 zone->uz_allocs = 0;
1704 zone->uz_sleeps = 0;
1706 zone->uz_count_min = 0;
1708 zone->uz_warning = NULL;
1709 /* The domain structures follow the cpu structures. */
1710 zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus];
1711 timevalclear(&zone->uz_ratecheck);
1714 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1717 * This is a pure cache zone, no kegs.
1720 if (arg->flags & UMA_ZONE_VM)
1721 arg->flags |= UMA_ZFLAG_CACHEONLY;
1722 zone->uz_flags = arg->flags;
1723 zone->uz_size = arg->size;
1724 zone->uz_import = arg->import;
1725 zone->uz_release = arg->release;
1726 zone->uz_arg = arg->arg;
1727 zone->uz_lockptr = &zone->uz_lock;
1728 rw_wlock(&uma_rwlock);
1729 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1730 rw_wunlock(&uma_rwlock);
1735 * Use the regular zone/keg/slab allocator.
1737 zone->uz_import = (uma_import)zone_import;
1738 zone->uz_release = (uma_release)zone_release;
1739 zone->uz_arg = zone;
1741 if (arg->flags & UMA_ZONE_SECONDARY) {
1742 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1743 zone->uz_init = arg->uminit;
1744 zone->uz_fini = arg->fini;
1745 zone->uz_lockptr = &keg->uk_lock;
1746 zone->uz_flags |= UMA_ZONE_SECONDARY;
1747 rw_wlock(&uma_rwlock);
1749 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1750 if (LIST_NEXT(z, uz_link) == NULL) {
1751 LIST_INSERT_AFTER(z, zone, uz_link);
1756 rw_wunlock(&uma_rwlock);
1757 } else if (keg == NULL) {
1758 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1759 arg->align, arg->flags)) == NULL)
1762 struct uma_kctor_args karg;
1765 /* We should only be here from uma_startup() */
1766 karg.size = arg->size;
1767 karg.uminit = arg->uminit;
1768 karg.fini = arg->fini;
1769 karg.align = arg->align;
1770 karg.flags = arg->flags;
1772 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1779 * Link in the first keg.
1781 zone->uz_klink.kl_keg = keg;
1782 LIST_INSERT_HEAD(&zone->uz_kegs, &zone->uz_klink, kl_link);
1783 zone->uz_lockptr = &keg->uk_lock;
1784 zone->uz_size = keg->uk_size;
1785 zone->uz_flags |= (keg->uk_flags &
1786 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1789 * Some internal zones don't have room allocated for the per cpu
1790 * caches. If we're internal, bail out here.
1792 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1793 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1794 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1799 KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
1800 (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
1801 ("Invalid zone flag combination"));
1802 if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
1803 zone->uz_count = BUCKET_MAX;
1804 else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
1807 zone->uz_count = bucket_select(zone->uz_size);
1808 zone->uz_count_min = zone->uz_count;
1814 * Keg header dtor. This frees all data, destroys locks, frees the hash
1815 * table and removes the keg from the global list.
1817 * Arguments/Returns follow uma_dtor specifications
1821 keg_dtor(void *arg, int size, void *udata)
1825 keg = (uma_keg_t)arg;
1827 if (keg->uk_free != 0) {
1828 printf("Freed UMA keg (%s) was not empty (%d items). "
1829 " Lost %d pages of memory.\n",
1830 keg->uk_name ? keg->uk_name : "",
1831 keg->uk_free, keg->uk_pages);
1835 hash_free(&keg->uk_hash);
1843 * Arguments/Returns follow uma_dtor specifications
1847 zone_dtor(void *arg, int size, void *udata)
1853 zone = (uma_zone_t)arg;
1854 keg = zone_first_keg(zone);
1856 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1859 rw_wlock(&uma_rwlock);
1860 LIST_REMOVE(zone, uz_link);
1861 rw_wunlock(&uma_rwlock);
1863 * XXX there are some races here where
1864 * the zone can be drained but zone lock
1865 * released and then refilled before we
1866 * remove it... we dont care for now
1868 zone_drain_wait(zone, M_WAITOK);
1870 * Unlink all of our kegs.
1872 while ((klink = LIST_FIRST(&zone->uz_kegs)) != NULL) {
1873 klink->kl_keg = NULL;
1874 LIST_REMOVE(klink, kl_link);
1875 if (klink == &zone->uz_klink)
1877 free(klink, M_TEMP);
1880 * We only destroy kegs from non secondary zones.
1882 if (keg != NULL && (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1883 rw_wlock(&uma_rwlock);
1884 LIST_REMOVE(keg, uk_link);
1885 rw_wunlock(&uma_rwlock);
1886 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1888 ZONE_LOCK_FINI(zone);
1892 * Traverses every zone in the system and calls a callback
1895 * zfunc A pointer to a function which accepts a zone
1902 zone_foreach(void (*zfunc)(uma_zone_t))
1907 rw_rlock(&uma_rwlock);
1908 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1909 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1912 rw_runlock(&uma_rwlock);
1916 * Count how many pages do we need to bootstrap. VM supplies
1917 * its need in early zones in the argument, we add up our zones,
1918 * which consist of: UMA Slabs, UMA Hash and 9 Bucket zones. The
1919 * zone of zones and zone of kegs are accounted separately.
1921 #define UMA_BOOT_ZONES 11
1922 /* Zone of zones and zone of kegs have arbitrary alignment. */
1923 #define UMA_BOOT_ALIGN 32
1924 static int zsize, ksize;
1926 uma_startup_count(int vm_zones)
1930 ksize = sizeof(struct uma_keg) +
1931 (sizeof(struct uma_domain) * vm_ndomains);
1932 zsize = sizeof(struct uma_zone) +
1933 (sizeof(struct uma_cache) * (mp_maxid + 1)) +
1934 (sizeof(struct uma_zone_domain) * vm_ndomains);
1937 * Memory for the zone of kegs and its keg,
1938 * and for zone of zones.
1940 pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 +
1941 roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE);
1943 #ifdef UMA_MD_SMALL_ALLOC
1944 zones = UMA_BOOT_ZONES;
1946 zones = UMA_BOOT_ZONES + vm_zones;
1950 /* Memory for the rest of startup zones, UMA and VM, ... */
1951 if (zsize > UMA_SLAB_SPACE)
1952 pages += (zones + vm_zones) *
1953 howmany(roundup2(zsize, UMA_BOOT_ALIGN), UMA_SLAB_SIZE);
1954 else if (roundup2(zsize, UMA_BOOT_ALIGN) > UMA_SLAB_SPACE)
1957 pages += howmany(zones,
1958 UMA_SLAB_SPACE / roundup2(zsize, UMA_BOOT_ALIGN));
1960 /* ... and their kegs. Note that zone of zones allocates a keg! */
1961 pages += howmany(zones + 1,
1962 UMA_SLAB_SPACE / roundup2(ksize, UMA_BOOT_ALIGN));
1965 * Most of startup zones are not going to be offpages, that's
1966 * why we use UMA_SLAB_SPACE instead of UMA_SLAB_SIZE in all
1967 * calculations. Some large bucket zones will be offpage, and
1968 * thus will allocate hashes. We take conservative approach
1969 * and assume that all zones may allocate hash. This may give
1970 * us some positive inaccuracy, usually an extra single page.
1972 pages += howmany(zones, UMA_SLAB_SPACE /
1973 (sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT));
1979 uma_startup(void *mem, int npages)
1981 struct uma_zctor_args args;
1982 uma_keg_t masterkeg;
1986 printf("Entering %s with %d boot pages configured\n", __func__, npages);
1989 rw_init(&uma_rwlock, "UMA lock");
1991 /* Use bootpages memory for the zone of zones and zone of kegs. */
1993 zones = (uma_zone_t)m;
1994 m += roundup(zsize, CACHE_LINE_SIZE);
1995 kegs = (uma_zone_t)m;
1996 m += roundup(zsize, CACHE_LINE_SIZE);
1997 masterkeg = (uma_keg_t)m;
1998 m += roundup(ksize, CACHE_LINE_SIZE);
1999 m = roundup(m, PAGE_SIZE);
2000 npages -= (m - (uintptr_t)mem) / PAGE_SIZE;
2003 /* "manually" create the initial zone */
2004 memset(&args, 0, sizeof(args));
2005 args.name = "UMA Kegs";
2007 args.ctor = keg_ctor;
2008 args.dtor = keg_dtor;
2009 args.uminit = zero_init;
2011 args.keg = masterkeg;
2012 args.align = UMA_BOOT_ALIGN - 1;
2013 args.flags = UMA_ZFLAG_INTERNAL;
2014 zone_ctor(kegs, zsize, &args, M_WAITOK);
2017 boot_pages = npages;
2019 args.name = "UMA Zones";
2021 args.ctor = zone_ctor;
2022 args.dtor = zone_dtor;
2023 args.uminit = zero_init;
2026 args.align = UMA_BOOT_ALIGN - 1;
2027 args.flags = UMA_ZFLAG_INTERNAL;
2028 zone_ctor(zones, zsize, &args, M_WAITOK);
2030 /* Now make a zone for slab headers */
2031 slabzone = uma_zcreate("UMA Slabs",
2032 sizeof(struct uma_slab),
2033 NULL, NULL, NULL, NULL,
2034 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2036 hashzone = uma_zcreate("UMA Hash",
2037 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
2038 NULL, NULL, NULL, NULL,
2039 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2043 booted = BOOT_STRAPPED;
2051 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2053 booted = BOOT_PAGEALLOC;
2061 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2063 booted = BOOT_BUCKETS;
2064 sx_init(&uma_drain_lock, "umadrain");
2069 * Initialize our callout handle
2077 TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2078 uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2079 uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2081 callout_init(&uma_callout, 1);
2082 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2083 booted = BOOT_RUNNING;
2087 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
2088 int align, uint32_t flags)
2090 struct uma_kctor_args args;
2093 args.uminit = uminit;
2095 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2098 return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2101 /* Public functions */
2104 uma_set_align(int align)
2107 if (align != UMA_ALIGN_CACHE)
2108 uma_align_cache = align;
2113 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
2114 uma_init uminit, uma_fini fini, int align, uint32_t flags)
2117 struct uma_zctor_args args;
2121 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2124 /* This stuff is essential for the zone ctor */
2125 memset(&args, 0, sizeof(args));
2130 args.uminit = uminit;
2134 * If a zone is being created with an empty constructor and
2135 * destructor, pass UMA constructor/destructor which checks for
2136 * memory use after free.
2138 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
2139 ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
2140 args.ctor = trash_ctor;
2141 args.dtor = trash_dtor;
2142 args.uminit = trash_init;
2143 args.fini = trash_fini;
2150 if (booted < BOOT_BUCKETS) {
2153 sx_slock(&uma_drain_lock);
2156 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2158 sx_sunlock(&uma_drain_lock);
2164 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
2165 uma_init zinit, uma_fini zfini, uma_zone_t master)
2167 struct uma_zctor_args args;
2172 keg = zone_first_keg(master);
2173 memset(&args, 0, sizeof(args));
2175 args.size = keg->uk_size;
2178 args.uminit = zinit;
2180 args.align = keg->uk_align;
2181 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2184 if (booted < BOOT_BUCKETS) {
2187 sx_slock(&uma_drain_lock);
2190 /* XXX Attaches only one keg of potentially many. */
2191 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2193 sx_sunlock(&uma_drain_lock);
2199 uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
2200 uma_init zinit, uma_fini zfini, uma_import zimport,
2201 uma_release zrelease, void *arg, int flags)
2203 struct uma_zctor_args args;
2205 memset(&args, 0, sizeof(args));
2210 args.uminit = zinit;
2212 args.import = zimport;
2213 args.release = zrelease;
2218 return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
2222 zone_lock_pair(uma_zone_t a, uma_zone_t b)
2226 mtx_lock_flags(b->uz_lockptr, MTX_DUPOK);
2229 mtx_lock_flags(a->uz_lockptr, MTX_DUPOK);
2234 zone_unlock_pair(uma_zone_t a, uma_zone_t b)
2242 uma_zsecond_add(uma_zone_t zone, uma_zone_t master)
2249 klink = malloc(sizeof(*klink), M_TEMP, M_WAITOK | M_ZERO);
2251 zone_lock_pair(zone, master);
2253 * zone must use vtoslab() to resolve objects and must already be
2256 if ((zone->uz_flags & (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY))
2257 != (UMA_ZONE_VTOSLAB | UMA_ZONE_SECONDARY)) {
2262 * The new master must also use vtoslab().
2264 if ((zone->uz_flags & UMA_ZONE_VTOSLAB) != UMA_ZONE_VTOSLAB) {
2270 * The underlying object must be the same size. rsize
2273 if (master->uz_size != zone->uz_size) {
2278 * Put it at the end of the list.
2280 klink->kl_keg = zone_first_keg(master);
2281 LIST_FOREACH(kl, &zone->uz_kegs, kl_link) {
2282 if (LIST_NEXT(kl, kl_link) == NULL) {
2283 LIST_INSERT_AFTER(kl, klink, kl_link);
2288 zone->uz_flags |= UMA_ZFLAG_MULTI;
2289 zone->uz_slab = zone_fetch_slab_multi;
2292 zone_unlock_pair(zone, master);
2294 free(klink, M_TEMP);
2302 uma_zdestroy(uma_zone_t zone)
2305 sx_slock(&uma_drain_lock);
2306 zone_free_item(zones, zone, NULL, SKIP_NONE);
2307 sx_sunlock(&uma_drain_lock);
2311 uma_zwait(uma_zone_t zone)
2315 item = uma_zalloc_arg(zone, NULL, M_WAITOK);
2316 uma_zfree(zone, item);
2320 uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
2326 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2328 item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
2329 if (item != NULL && (flags & M_ZERO)) {
2331 for (i = 0; i <= mp_maxid; i++)
2332 bzero(zpcpu_get_cpu(item, i), zone->uz_size);
2334 bzero(item, zone->uz_size);
2341 * A stub while both regular and pcpu cases are identical.
2344 uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata)
2348 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2350 uma_zfree_arg(zone, item, udata);
2355 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
2357 uma_zone_domain_t zdom;
2358 uma_bucket_t bucket;
2361 int cpu, domain, lockfail;
2366 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2367 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2369 /* This is the fast path allocation */
2370 CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
2371 curthread, zone->uz_name, zone, flags);
2373 if (flags & M_WAITOK) {
2374 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2375 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
2377 KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC"));
2378 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2379 ("uma_zalloc_arg: called with spinlock or critical section held"));
2380 if (zone->uz_flags & UMA_ZONE_PCPU)
2381 KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone "
2382 "with M_ZERO passed"));
2384 #ifdef DEBUG_MEMGUARD
2385 if (memguard_cmp_zone(zone)) {
2386 item = memguard_alloc(zone->uz_size, flags);
2388 if (zone->uz_init != NULL &&
2389 zone->uz_init(item, zone->uz_size, flags) != 0)
2391 if (zone->uz_ctor != NULL &&
2392 zone->uz_ctor(item, zone->uz_size, udata,
2394 zone->uz_fini(item, zone->uz_size);
2399 /* This is unfortunate but should not be fatal. */
2403 * If possible, allocate from the per-CPU cache. There are two
2404 * requirements for safe access to the per-CPU cache: (1) the thread
2405 * accessing the cache must not be preempted or yield during access,
2406 * and (2) the thread must not migrate CPUs without switching which
2407 * cache it accesses. We rely on a critical section to prevent
2408 * preemption and migration. We release the critical section in
2409 * order to acquire the zone mutex if we are unable to allocate from
2410 * the current cache; when we re-acquire the critical section, we
2411 * must detect and handle migration if it has occurred.
2415 cache = &zone->uz_cpu[cpu];
2418 bucket = cache->uc_allocbucket;
2419 if (bucket != NULL && bucket->ub_cnt > 0) {
2421 item = bucket->ub_bucket[bucket->ub_cnt];
2423 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2425 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2429 skipdbg = uma_dbg_zskip(zone, item);
2431 if (zone->uz_ctor != NULL &&
2433 (!skipdbg || zone->uz_ctor != trash_ctor ||
2434 zone->uz_dtor != trash_dtor) &&
2436 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2437 atomic_add_long(&zone->uz_fails, 1);
2438 zone_free_item(zone, item, udata, SKIP_DTOR);
2443 uma_dbg_alloc(zone, NULL, item);
2446 uma_zero_item(item, zone);
2451 * We have run out of items in our alloc bucket.
2452 * See if we can switch with our free bucket.
2454 bucket = cache->uc_freebucket;
2455 if (bucket != NULL && bucket->ub_cnt > 0) {
2457 "uma_zalloc: zone %s(%p) swapping empty with alloc",
2458 zone->uz_name, zone);
2459 cache->uc_freebucket = cache->uc_allocbucket;
2460 cache->uc_allocbucket = bucket;
2465 * Discard any empty allocation bucket while we hold no locks.
2467 bucket = cache->uc_allocbucket;
2468 cache->uc_allocbucket = NULL;
2471 bucket_free(zone, bucket, udata);
2473 if (zone->uz_flags & UMA_ZONE_NUMA) {
2474 domain = PCPU_GET(domain);
2475 if (VM_DOMAIN_EMPTY(domain))
2476 domain = UMA_ANYDOMAIN;
2478 domain = UMA_ANYDOMAIN;
2480 /* Short-circuit for zones without buckets and low memory. */
2481 if (zone->uz_count == 0 || bucketdisable)
2485 * Attempt to retrieve the item from the per-CPU cache has failed, so
2486 * we must go back to the zone. This requires the zone lock, so we
2487 * must drop the critical section, then re-acquire it when we go back
2488 * to the cache. Since the critical section is released, we may be
2489 * preempted or migrate. As such, make sure not to maintain any
2490 * thread-local state specific to the cache from prior to releasing
2491 * the critical section.
2494 if (ZONE_TRYLOCK(zone) == 0) {
2495 /* Record contention to size the buckets. */
2501 cache = &zone->uz_cpu[cpu];
2503 /* See if we lost the race to fill the cache. */
2504 if (cache->uc_allocbucket != NULL) {
2510 * Check the zone's cache of buckets.
2512 if (domain == UMA_ANYDOMAIN)
2513 zdom = &zone->uz_domain[0];
2515 zdom = &zone->uz_domain[domain];
2516 if ((bucket = LIST_FIRST(&zdom->uzd_buckets)) != NULL) {
2517 KASSERT(bucket->ub_cnt != 0,
2518 ("uma_zalloc_arg: Returning an empty bucket."));
2520 LIST_REMOVE(bucket, ub_link);
2521 cache->uc_allocbucket = bucket;
2525 /* We are no longer associated with this CPU. */
2529 * We bump the uz count when the cache size is insufficient to
2530 * handle the working set.
2532 if (lockfail && zone->uz_count < BUCKET_MAX)
2537 * Now lets just fill a bucket and put it on the free list. If that
2538 * works we'll restart the allocation from the beginning and it
2539 * will use the just filled bucket.
2541 bucket = zone_alloc_bucket(zone, udata, domain, flags);
2542 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
2543 zone->uz_name, zone, bucket);
2544 if (bucket != NULL) {
2548 cache = &zone->uz_cpu[cpu];
2550 * See if we lost the race or were migrated. Cache the
2551 * initialized bucket to make this less likely or claim
2552 * the memory directly.
2554 if (cache->uc_allocbucket != NULL ||
2555 (zone->uz_flags & UMA_ZONE_NUMA &&
2556 domain != PCPU_GET(domain)))
2557 LIST_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link);
2559 cache->uc_allocbucket = bucket;
2565 * We may not be able to get a bucket so return an actual item.
2568 item = zone_alloc_item(zone, udata, domain, flags);
2574 uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
2577 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2578 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2580 /* This is the fast path allocation */
2582 "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d",
2583 curthread, zone->uz_name, zone, domain, flags);
2585 if (flags & M_WAITOK) {
2586 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2587 "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
2589 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2590 ("uma_zalloc_domain: called with spinlock or critical section held"));
2592 return (zone_alloc_item(zone, udata, domain, flags));
2596 * Find a slab with some space. Prefer slabs that are partially used over those
2597 * that are totally full. This helps to reduce fragmentation.
2599 * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check
2603 keg_first_slab(uma_keg_t keg, int domain, int rr)
2609 KASSERT(domain >= 0 && domain < vm_ndomains,
2610 ("keg_first_slab: domain %d out of range", domain));
2615 dom = &keg->uk_domain[domain];
2616 if (!LIST_EMPTY(&dom->ud_part_slab))
2617 return (LIST_FIRST(&dom->ud_part_slab));
2618 if (!LIST_EMPTY(&dom->ud_free_slab)) {
2619 slab = LIST_FIRST(&dom->ud_free_slab);
2620 LIST_REMOVE(slab, us_link);
2621 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2625 domain = (domain + 1) % vm_ndomains;
2626 } while (domain != start);
2632 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, int flags)
2636 int allocflags, domain, reserve, rr, start;
2638 mtx_assert(&keg->uk_lock, MA_OWNED);
2642 if ((flags & M_USE_RESERVE) == 0)
2643 reserve = keg->uk_reserve;
2646 * Round-robin for non first-touch zones when there is more than one
2649 if (vm_ndomains == 1)
2651 rr = rdomain == UMA_ANYDOMAIN;
2653 start = keg->uk_cursor;
2655 keg->uk_cursor = (keg->uk_cursor + 1) % vm_ndomains;
2656 domain = keg->uk_cursor;
2657 } while (VM_DOMAIN_EMPTY(domain) && domain != start);
2658 domain = start = keg->uk_cursor;
2659 /* Only block on the second pass. */
2660 if ((flags & (M_WAITOK | M_NOVM)) == M_WAITOK)
2661 allocflags = (allocflags & ~M_WAITOK) | M_NOWAIT;
2663 domain = start = rdomain;
2667 if (keg->uk_free > reserve &&
2668 (slab = keg_first_slab(keg, domain, rr)) != NULL) {
2669 MPASS(slab->us_keg == keg);
2674 * M_NOVM means don't ask at all!
2679 if (keg->uk_maxpages && keg->uk_pages >= keg->uk_maxpages) {
2680 keg->uk_flags |= UMA_ZFLAG_FULL;
2682 * If this is not a multi-zone, set the FULL bit.
2683 * Otherwise slab_multi() takes care of it.
2685 if ((zone->uz_flags & UMA_ZFLAG_MULTI) == 0) {
2686 zone->uz_flags |= UMA_ZFLAG_FULL;
2687 zone_log_warning(zone);
2688 zone_maxaction(zone);
2690 if (flags & M_NOWAIT)
2693 msleep(keg, &keg->uk_lock, PVM, "keglimit", 0);
2696 slab = keg_alloc_slab(keg, zone, domain, allocflags);
2698 * If we got a slab here it's safe to mark it partially used
2699 * and return. We assume that the caller is going to remove
2700 * at least one item.
2703 MPASS(slab->us_keg == keg);
2704 dom = &keg->uk_domain[slab->us_domain];
2705 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2710 domain = (domain + 1) % vm_ndomains;
2711 } while (VM_DOMAIN_EMPTY(domain) && domain != start);
2713 } while (domain != start);
2715 /* Retry domain scan with blocking. */
2716 if (allocflags != flags) {
2722 * We might not have been able to get a slab but another cpu
2723 * could have while we were unlocked. Check again before we
2726 if (keg->uk_free > reserve &&
2727 (slab = keg_first_slab(keg, domain, rr)) != NULL) {
2728 MPASS(slab->us_keg == keg);
2735 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int domain, int flags)
2740 keg = zone_first_keg(zone);
2745 slab = keg_fetch_slab(keg, zone, domain, flags);
2748 if (flags & (M_NOWAIT | M_NOVM))
2756 * uma_zone_fetch_slab_multi: Fetches a slab from one available keg. Returns
2757 * with the keg locked. On NULL no lock is held.
2759 * The last pointer is used to seed the search. It is not required.
2762 zone_fetch_slab_multi(uma_zone_t zone, uma_keg_t last, int domain, int rflags)
2772 * Don't wait on the first pass. This will skip limit tests
2773 * as well. We don't want to block if we can find a provider
2776 flags = (rflags & ~M_WAITOK) | M_NOWAIT;
2778 * Use the last slab allocated as a hint for where to start
2782 slab = keg_fetch_slab(last, zone, domain, flags);
2788 * Loop until we have a slab incase of transient failures
2789 * while M_WAITOK is specified. I'm not sure this is 100%
2790 * required but we've done it for so long now.
2796 * Search the available kegs for slabs. Be careful to hold the
2797 * correct lock while calling into the keg layer.
2799 LIST_FOREACH(klink, &zone->uz_kegs, kl_link) {
2800 keg = klink->kl_keg;
2802 if ((keg->uk_flags & UMA_ZFLAG_FULL) == 0) {
2803 slab = keg_fetch_slab(keg, zone, domain, flags);
2807 if (keg->uk_flags & UMA_ZFLAG_FULL)
2813 if (rflags & (M_NOWAIT | M_NOVM))
2817 * All kegs are full. XXX We can't atomically check all kegs
2818 * and sleep so just sleep for a short period and retry.
2820 if (full && !empty) {
2822 zone->uz_flags |= UMA_ZFLAG_FULL;
2824 zone_log_warning(zone);
2825 zone_maxaction(zone);
2826 msleep(zone, zone->uz_lockptr, PVM,
2827 "zonelimit", hz/100);
2828 zone->uz_flags &= ~UMA_ZFLAG_FULL;
2837 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2843 MPASS(keg == slab->us_keg);
2844 mtx_assert(&keg->uk_lock, MA_OWNED);
2846 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2847 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2848 item = slab->us_data + (keg->uk_rsize * freei);
2849 slab->us_freecount--;
2852 /* Move this slab to the full list */
2853 if (slab->us_freecount == 0) {
2854 LIST_REMOVE(slab, us_link);
2855 dom = &keg->uk_domain[slab->us_domain];
2856 LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
2863 zone_import(uma_zone_t zone, void **bucket, int max, int domain, int flags)
2874 /* Try to keep the buckets totally full */
2875 for (i = 0; i < max; ) {
2876 if ((slab = zone->uz_slab(zone, keg, domain, flags)) == NULL)
2880 stripe = howmany(max, vm_ndomains);
2882 while (slab->us_freecount && i < max) {
2883 bucket[i++] = slab_alloc_item(keg, slab);
2884 if (keg->uk_free <= keg->uk_reserve)
2888 * If the zone is striped we pick a new slab for every
2889 * N allocations. Eliminating this conditional will
2890 * instead pick a new domain for each bucket rather
2891 * than stripe within each bucket. The current option
2892 * produces more fragmentation and requires more cpu
2893 * time but yields better distribution.
2895 if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 &&
2896 vm_ndomains > 1 && --stripe == 0)
2900 /* Don't block if we allocated any successfully. */
2911 zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags)
2913 uma_bucket_t bucket;
2916 CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain);
2918 /* Don't wait for buckets, preserve caller's NOVM setting. */
2919 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
2923 max = MIN(bucket->ub_entries, zone->uz_count);
2924 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
2925 max, domain, flags);
2928 * Initialize the memory if necessary.
2930 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2933 for (i = 0; i < bucket->ub_cnt; i++)
2934 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2938 * If we couldn't initialize the whole bucket, put the
2939 * rest back onto the freelist.
2941 if (i != bucket->ub_cnt) {
2942 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
2943 bucket->ub_cnt - i);
2945 bzero(&bucket->ub_bucket[i],
2946 sizeof(void *) * (bucket->ub_cnt - i));
2952 if (bucket->ub_cnt == 0) {
2953 bucket_free(zone, bucket, udata);
2954 atomic_add_long(&zone->uz_fails, 1);
2962 * Allocates a single item from a zone.
2965 * zone The zone to alloc for.
2966 * udata The data to be passed to the constructor.
2967 * domain The domain to allocate from or UMA_ANYDOMAIN.
2968 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2971 * NULL if there is no memory and M_NOWAIT is set
2972 * An item if successful
2976 zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
2985 if (domain != UMA_ANYDOMAIN) {
2986 /* avoid allocs targeting empty domains */
2987 if (VM_DOMAIN_EMPTY(domain))
2988 domain = UMA_ANYDOMAIN;
2990 if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
2992 atomic_add_long(&zone->uz_allocs, 1);
2995 skipdbg = uma_dbg_zskip(zone, item);
2998 * We have to call both the zone's init (not the keg's init)
2999 * and the zone's ctor. This is because the item is going from
3000 * a keg slab directly to the user, and the user is expecting it
3001 * to be both zone-init'd as well as zone-ctor'd.
3003 if (zone->uz_init != NULL) {
3004 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
3005 zone_free_item(zone, item, udata, SKIP_FINI);
3009 if (zone->uz_ctor != NULL &&
3011 (!skipdbg || zone->uz_ctor != trash_ctor ||
3012 zone->uz_dtor != trash_dtor) &&
3014 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
3015 zone_free_item(zone, item, udata, SKIP_DTOR);
3020 uma_dbg_alloc(zone, NULL, item);
3023 uma_zero_item(item, zone);
3025 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
3026 zone->uz_name, zone);
3031 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
3032 zone->uz_name, zone);
3033 atomic_add_long(&zone->uz_fails, 1);
3039 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
3042 uma_bucket_t bucket;
3043 uma_zone_domain_t zdom;
3044 int cpu, domain, lockfail;
3049 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3050 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3052 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
3055 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3056 ("uma_zfree_arg: called with spinlock or critical section held"));
3058 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3061 #ifdef DEBUG_MEMGUARD
3062 if (is_memguard_addr(item)) {
3063 if (zone->uz_dtor != NULL)
3064 zone->uz_dtor(item, zone->uz_size, udata);
3065 if (zone->uz_fini != NULL)
3066 zone->uz_fini(item, zone->uz_size);
3067 memguard_free(item);
3072 skipdbg = uma_dbg_zskip(zone, item);
3073 if (skipdbg == false) {
3074 if (zone->uz_flags & UMA_ZONE_MALLOC)
3075 uma_dbg_free(zone, udata, item);
3077 uma_dbg_free(zone, NULL, item);
3079 if (zone->uz_dtor != NULL && (!skipdbg ||
3080 zone->uz_dtor != trash_dtor || zone->uz_ctor != trash_ctor))
3082 if (zone->uz_dtor != NULL)
3084 zone->uz_dtor(item, zone->uz_size, udata);
3087 * The race here is acceptable. If we miss it we'll just have to wait
3088 * a little longer for the limits to be reset.
3090 if (zone->uz_flags & UMA_ZFLAG_FULL)
3094 * If possible, free to the per-CPU cache. There are two
3095 * requirements for safe access to the per-CPU cache: (1) the thread
3096 * accessing the cache must not be preempted or yield during access,
3097 * and (2) the thread must not migrate CPUs without switching which
3098 * cache it accesses. We rely on a critical section to prevent
3099 * preemption and migration. We release the critical section in
3100 * order to acquire the zone mutex if we are unable to free to the
3101 * current cache; when we re-acquire the critical section, we must
3102 * detect and handle migration if it has occurred.
3107 cache = &zone->uz_cpu[cpu];
3111 * Try to free into the allocbucket first to give LIFO ordering
3112 * for cache-hot datastructures. Spill over into the freebucket
3113 * if necessary. Alloc will swap them if one runs dry.
3115 bucket = cache->uc_allocbucket;
3116 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
3117 bucket = cache->uc_freebucket;
3118 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3119 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
3120 ("uma_zfree: Freeing to non free bucket index."));
3121 bucket->ub_bucket[bucket->ub_cnt] = item;
3129 * We must go back the zone, which requires acquiring the zone lock,
3130 * which in turn means we must release and re-acquire the critical
3131 * section. Since the critical section is released, we may be
3132 * preempted or migrate. As such, make sure not to maintain any
3133 * thread-local state specific to the cache from prior to releasing
3134 * the critical section.
3137 if (zone->uz_count == 0 || bucketdisable)
3141 if (ZONE_TRYLOCK(zone) == 0) {
3142 /* Record contention to size the buckets. */
3148 cache = &zone->uz_cpu[cpu];
3150 bucket = cache->uc_freebucket;
3151 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3155 cache->uc_freebucket = NULL;
3156 /* We are no longer associated with this CPU. */
3159 if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) {
3160 domain = PCPU_GET(domain);
3161 if (VM_DOMAIN_EMPTY(domain))
3162 domain = UMA_ANYDOMAIN;
3165 zdom = &zone->uz_domain[0];
3167 /* Can we throw this on the zone full list? */
3168 if (bucket != NULL) {
3170 "uma_zfree: zone %s(%p) putting bucket %p on free list",
3171 zone->uz_name, zone, bucket);
3172 /* ub_cnt is pointing to the last free item */
3173 KASSERT(bucket->ub_cnt != 0,
3174 ("uma_zfree: Attempting to insert an empty bucket onto the full list.\n"));
3175 if ((zone->uz_flags & UMA_ZONE_NOBUCKETCACHE) != 0) {
3177 bucket_drain(zone, bucket);
3178 bucket_free(zone, bucket, udata);
3181 LIST_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link);
3185 * We bump the uz count when the cache size is insufficient to
3186 * handle the working set.
3188 if (lockfail && zone->uz_count < BUCKET_MAX)
3192 bucket = bucket_alloc(zone, udata, M_NOWAIT);
3193 CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p",
3194 zone->uz_name, zone, bucket);
3198 cache = &zone->uz_cpu[cpu];
3199 if (cache->uc_freebucket == NULL &&
3200 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
3201 domain == PCPU_GET(domain))) {
3202 cache->uc_freebucket = bucket;
3206 * We lost the race, start over. We have to drop our
3207 * critical section to free the bucket.
3210 bucket_free(zone, bucket, udata);
3215 * If nothing else caught this, we'll just do an internal free.
3218 zone_free_item(zone, item, udata, SKIP_DTOR);
3224 uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
3227 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3228 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3230 CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread,
3233 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3234 ("uma_zfree_domain: called with spinlock or critical section held"));
3236 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3239 zone_free_item(zone, item, udata, SKIP_NONE);
3243 slab_free_item(uma_keg_t keg, uma_slab_t slab, void *item)
3248 mtx_assert(&keg->uk_lock, MA_OWNED);
3249 MPASS(keg == slab->us_keg);
3251 dom = &keg->uk_domain[slab->us_domain];
3253 /* Do we need to remove from any lists? */
3254 if (slab->us_freecount+1 == keg->uk_ipers) {
3255 LIST_REMOVE(slab, us_link);
3256 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
3257 } else if (slab->us_freecount == 0) {
3258 LIST_REMOVE(slab, us_link);
3259 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3262 /* Slab management. */
3263 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3264 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
3265 slab->us_freecount++;
3267 /* Keg statistics. */
3272 zone_release(uma_zone_t zone, void **bucket, int cnt)
3282 keg = zone_first_keg(zone);
3284 for (i = 0; i < cnt; i++) {
3286 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
3287 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
3288 if (zone->uz_flags & UMA_ZONE_HASH) {
3289 slab = hash_sfind(&keg->uk_hash, mem);
3291 mem += keg->uk_pgoff;
3292 slab = (uma_slab_t)mem;
3295 slab = vtoslab((vm_offset_t)item);
3296 if (slab->us_keg != keg) {
3302 slab_free_item(keg, slab, item);
3303 if (keg->uk_flags & UMA_ZFLAG_FULL) {
3304 if (keg->uk_pages < keg->uk_maxpages) {
3305 keg->uk_flags &= ~UMA_ZFLAG_FULL;
3310 * We can handle one more allocation. Since we're
3311 * clearing ZFLAG_FULL, wake up all procs blocked
3312 * on pages. This should be uncommon, so keeping this
3313 * simple for now (rather than adding count of blocked
3322 zone->uz_flags &= ~UMA_ZFLAG_FULL;
3330 * Frees a single item to any zone.
3333 * zone The zone to free to
3334 * item The item we're freeing
3335 * udata User supplied data for the dtor
3336 * skip Skip dtors and finis
3339 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
3344 skipdbg = uma_dbg_zskip(zone, item);
3345 if (skip == SKIP_NONE && !skipdbg) {
3346 if (zone->uz_flags & UMA_ZONE_MALLOC)
3347 uma_dbg_free(zone, udata, item);
3349 uma_dbg_free(zone, NULL, item);
3352 if (skip < SKIP_DTOR && zone->uz_dtor != NULL &&
3353 (!skipdbg || zone->uz_dtor != trash_dtor ||
3354 zone->uz_ctor != trash_ctor))
3356 if (skip < SKIP_DTOR && zone->uz_dtor != NULL)
3358 zone->uz_dtor(item, zone->uz_size, udata);
3360 if (skip < SKIP_FINI && zone->uz_fini)
3361 zone->uz_fini(item, zone->uz_size);
3363 atomic_add_long(&zone->uz_frees, 1);
3364 zone->uz_release(zone->uz_arg, &item, 1);
3369 uma_zone_set_max(uma_zone_t zone, int nitems)
3373 keg = zone_first_keg(zone);
3377 keg->uk_maxpages = (nitems / keg->uk_ipers) * keg->uk_ppera;
3378 if (keg->uk_maxpages * keg->uk_ipers < nitems)
3379 keg->uk_maxpages += keg->uk_ppera;
3380 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
3388 uma_zone_get_max(uma_zone_t zone)
3393 keg = zone_first_keg(zone);
3397 nitems = (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers;
3405 uma_zone_set_warning(uma_zone_t zone, const char *warning)
3409 zone->uz_warning = warning;
3415 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
3419 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
3425 uma_zone_get_cur(uma_zone_t zone)
3431 nitems = zone->uz_allocs - zone->uz_frees;
3434 * See the comment in sysctl_vm_zone_stats() regarding the
3435 * safety of accessing the per-cpu caches. With the zone lock
3436 * held, it is safe, but can potentially result in stale data.
3438 nitems += zone->uz_cpu[i].uc_allocs -
3439 zone->uz_cpu[i].uc_frees;
3443 return (nitems < 0 ? 0 : nitems);
3448 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
3452 keg = zone_first_keg(zone);
3453 KASSERT(keg != NULL, ("uma_zone_set_init: Invalid zone type"));
3455 KASSERT(keg->uk_pages == 0,
3456 ("uma_zone_set_init on non-empty keg"));
3457 keg->uk_init = uminit;
3463 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3467 keg = zone_first_keg(zone);
3468 KASSERT(keg != NULL, ("uma_zone_set_fini: Invalid zone type"));
3470 KASSERT(keg->uk_pages == 0,
3471 ("uma_zone_set_fini on non-empty keg"));
3472 keg->uk_fini = fini;
3478 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3482 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3483 ("uma_zone_set_zinit on non-empty keg"));
3484 zone->uz_init = zinit;
3490 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3494 KASSERT(zone_first_keg(zone)->uk_pages == 0,
3495 ("uma_zone_set_zfini on non-empty keg"));
3496 zone->uz_fini = zfini;
3501 /* XXX uk_freef is not actually used with the zone locked */
3503 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
3507 keg = zone_first_keg(zone);
3508 KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3510 keg->uk_freef = freef;
3515 /* XXX uk_allocf is not actually used with the zone locked */
3517 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
3521 keg = zone_first_keg(zone);
3523 keg->uk_allocf = allocf;
3529 uma_zone_reserve(uma_zone_t zone, int items)
3533 keg = zone_first_keg(zone);
3537 keg->uk_reserve = items;
3545 uma_zone_reserve_kva(uma_zone_t zone, int count)
3551 keg = zone_first_keg(zone);
3554 pages = count / keg->uk_ipers;
3556 if (pages * keg->uk_ipers < count)
3558 pages *= keg->uk_ppera;
3560 #ifdef UMA_MD_SMALL_ALLOC
3561 if (keg->uk_ppera > 1) {
3565 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3573 keg->uk_maxpages = pages;
3574 #ifdef UMA_MD_SMALL_ALLOC
3575 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3577 keg->uk_allocf = noobj_alloc;
3579 keg->uk_flags |= UMA_ZONE_NOFREE;
3587 uma_prealloc(uma_zone_t zone, int items)
3594 keg = zone_first_keg(zone);
3598 slabs = items / keg->uk_ipers;
3600 if (slabs * keg->uk_ipers < items)
3603 slab = keg_alloc_slab(keg, zone, domain, M_WAITOK);
3606 MPASS(slab->us_keg == keg);
3607 dom = &keg->uk_domain[slab->us_domain];
3608 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
3611 domain = (domain + 1) % vm_ndomains;
3612 } while (VM_DOMAIN_EMPTY(domain));
3619 uma_reclaim_locked(bool kmem_danger)
3622 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
3623 sx_assert(&uma_drain_lock, SA_XLOCKED);
3625 zone_foreach(zone_drain);
3626 if (vm_page_count_min() || kmem_danger) {
3627 cache_drain_safe(NULL);
3628 zone_foreach(zone_drain);
3631 * Some slabs may have been freed but this zone will be visited early
3632 * we visit again so that we can free pages that are empty once other
3633 * zones are drained. We have to do the same for buckets.
3635 zone_drain(slabzone);
3636 bucket_zone_drain();
3643 sx_xlock(&uma_drain_lock);
3644 uma_reclaim_locked(false);
3645 sx_xunlock(&uma_drain_lock);
3648 static volatile int uma_reclaim_needed;
3651 uma_reclaim_wakeup(void)
3654 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
3655 wakeup(uma_reclaim);
3659 uma_reclaim_worker(void *arg __unused)
3663 sx_xlock(&uma_drain_lock);
3664 while (atomic_load_int(&uma_reclaim_needed) == 0)
3665 sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl",
3667 sx_xunlock(&uma_drain_lock);
3668 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
3669 sx_xlock(&uma_drain_lock);
3670 uma_reclaim_locked(true);
3671 atomic_store_int(&uma_reclaim_needed, 0);
3672 sx_xunlock(&uma_drain_lock);
3673 /* Don't fire more than once per-second. */
3674 pause("umarclslp", hz);
3680 uma_zone_exhausted(uma_zone_t zone)
3685 full = (zone->uz_flags & UMA_ZFLAG_FULL);
3691 uma_zone_exhausted_nolock(uma_zone_t zone)
3693 return (zone->uz_flags & UMA_ZFLAG_FULL);
3697 uma_large_malloc_domain(vm_size_t size, int domain, int wait)
3702 if (domain != UMA_ANYDOMAIN) {
3703 /* avoid allocs targeting empty domains */
3704 if (VM_DOMAIN_EMPTY(domain))
3705 domain = UMA_ANYDOMAIN;
3707 slab = zone_alloc_item(slabzone, NULL, domain, wait);
3710 if (domain == UMA_ANYDOMAIN)
3711 addr = kmem_malloc(size, wait);
3713 addr = kmem_malloc_domain(domain, size, wait);
3715 vsetslab(addr, slab);
3716 slab->us_data = (void *)addr;
3717 slab->us_flags = UMA_SLAB_KERNEL | UMA_SLAB_MALLOC;
3718 slab->us_size = size;
3719 slab->us_domain = vm_phys_domain(PHYS_TO_VM_PAGE(
3720 pmap_kextract(addr)));
3721 uma_total_inc(size);
3723 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3726 return ((void *)addr);
3730 uma_large_malloc(vm_size_t size, int wait)
3733 return uma_large_malloc_domain(size, UMA_ANYDOMAIN, wait);
3737 uma_large_free(uma_slab_t slab)
3740 KASSERT((slab->us_flags & UMA_SLAB_KERNEL) != 0,
3741 ("uma_large_free: Memory not allocated with uma_large_malloc."));
3742 kmem_free((vm_offset_t)slab->us_data, slab->us_size);
3743 uma_total_dec(slab->us_size);
3744 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3748 uma_zero_item(void *item, uma_zone_t zone)
3751 bzero(item, zone->uz_size);
3758 return (uma_kmem_limit);
3762 uma_set_limit(unsigned long limit)
3765 uma_kmem_limit = limit;
3772 return (uma_kmem_total);
3779 return (uma_kmem_limit - uma_kmem_total);
3783 uma_print_stats(void)
3785 zone_foreach(uma_print_zone);
3789 slab_print(uma_slab_t slab)
3791 printf("slab: keg %p, data %p, freecount %d\n",
3792 slab->us_keg, slab->us_data, slab->us_freecount);
3796 cache_print(uma_cache_t cache)
3798 printf("alloc: %p(%d), free: %p(%d)\n",
3799 cache->uc_allocbucket,
3800 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3801 cache->uc_freebucket,
3802 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3806 uma_print_keg(uma_keg_t keg)
3812 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3813 "out %d free %d limit %d\n",
3814 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3815 keg->uk_ipers, keg->uk_ppera,
3816 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
3817 keg->uk_free, (keg->uk_maxpages / keg->uk_ppera) * keg->uk_ipers);
3818 for (i = 0; i < vm_ndomains; i++) {
3819 dom = &keg->uk_domain[i];
3820 printf("Part slabs:\n");
3821 LIST_FOREACH(slab, &dom->ud_part_slab, us_link)
3823 printf("Free slabs:\n");
3824 LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
3826 printf("Full slabs:\n");
3827 LIST_FOREACH(slab, &dom->ud_full_slab, us_link)
3833 uma_print_zone(uma_zone_t zone)
3839 printf("zone: %s(%p) size %d flags %#x\n",
3840 zone->uz_name, zone, zone->uz_size, zone->uz_flags);
3841 LIST_FOREACH(kl, &zone->uz_kegs, kl_link)
3842 uma_print_keg(kl->kl_keg);
3844 cache = &zone->uz_cpu[i];
3845 printf("CPU %d Cache:\n", i);
3852 * Generate statistics across both the zone and its per-cpu cache's. Return
3853 * desired statistics if the pointer is non-NULL for that statistic.
3855 * Note: does not update the zone statistics, as it can't safely clear the
3856 * per-CPU cache statistic.
3858 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3859 * safe from off-CPU; we should modify the caches to track this information
3860 * directly so that we don't have to.
3863 uma_zone_sumstat(uma_zone_t z, int *cachefreep, uint64_t *allocsp,
3864 uint64_t *freesp, uint64_t *sleepsp)
3867 uint64_t allocs, frees, sleeps;
3870 allocs = frees = sleeps = 0;
3873 cache = &z->uz_cpu[cpu];
3874 if (cache->uc_allocbucket != NULL)
3875 cachefree += cache->uc_allocbucket->ub_cnt;
3876 if (cache->uc_freebucket != NULL)
3877 cachefree += cache->uc_freebucket->ub_cnt;
3878 allocs += cache->uc_allocs;
3879 frees += cache->uc_frees;
3881 allocs += z->uz_allocs;
3882 frees += z->uz_frees;
3883 sleeps += z->uz_sleeps;
3884 if (cachefreep != NULL)
3885 *cachefreep = cachefree;
3886 if (allocsp != NULL)
3890 if (sleepsp != NULL)
3896 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3903 rw_rlock(&uma_rwlock);
3904 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3905 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3908 rw_runlock(&uma_rwlock);
3909 return (sysctl_handle_int(oidp, &count, 0, req));
3913 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3915 struct uma_stream_header ush;
3916 struct uma_type_header uth;
3917 struct uma_percpu_stat *ups;
3918 uma_bucket_t bucket;
3919 uma_zone_domain_t zdom;
3926 int count, error, i;
3928 error = sysctl_wire_old_buffer(req, 0);
3931 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3932 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
3933 ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
3936 rw_rlock(&uma_rwlock);
3937 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3938 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3943 * Insert stream header.
3945 bzero(&ush, sizeof(ush));
3946 ush.ush_version = UMA_STREAM_VERSION;
3947 ush.ush_maxcpus = (mp_maxid + 1);
3948 ush.ush_count = count;
3949 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3951 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3952 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3953 bzero(&uth, sizeof(uth));
3955 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3956 uth.uth_align = kz->uk_align;
3957 uth.uth_size = kz->uk_size;
3958 uth.uth_rsize = kz->uk_rsize;
3959 LIST_FOREACH(kl, &z->uz_kegs, kl_link) {
3961 uth.uth_maxpages += k->uk_maxpages;
3962 uth.uth_pages += k->uk_pages;
3963 uth.uth_keg_free += k->uk_free;
3964 uth.uth_limit = (k->uk_maxpages / k->uk_ppera)
3969 * A zone is secondary is it is not the first entry
3970 * on the keg's zone list.
3972 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3973 (LIST_FIRST(&kz->uk_zones) != z))
3974 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3976 for (i = 0; i < vm_ndomains; i++) {
3977 zdom = &z->uz_domain[i];
3978 LIST_FOREACH(bucket, &zdom->uzd_buckets,
3980 uth.uth_zone_free += bucket->ub_cnt;
3982 uth.uth_allocs = z->uz_allocs;
3983 uth.uth_frees = z->uz_frees;
3984 uth.uth_fails = z->uz_fails;
3985 uth.uth_sleeps = z->uz_sleeps;
3987 * While it is not normally safe to access the cache
3988 * bucket pointers while not on the CPU that owns the
3989 * cache, we only allow the pointers to be exchanged
3990 * without the zone lock held, not invalidated, so
3991 * accept the possible race associated with bucket
3992 * exchange during monitoring.
3994 for (i = 0; i < mp_maxid + 1; i++) {
3995 bzero(&ups[i], sizeof(*ups));
3996 if (kz->uk_flags & UMA_ZFLAG_INTERNAL ||
3999 cache = &z->uz_cpu[i];
4000 if (cache->uc_allocbucket != NULL)
4001 ups[i].ups_cache_free +=
4002 cache->uc_allocbucket->ub_cnt;
4003 if (cache->uc_freebucket != NULL)
4004 ups[i].ups_cache_free +=
4005 cache->uc_freebucket->ub_cnt;
4006 ups[i].ups_allocs = cache->uc_allocs;
4007 ups[i].ups_frees = cache->uc_frees;
4010 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4011 for (i = 0; i < mp_maxid + 1; i++)
4012 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4015 rw_runlock(&uma_rwlock);
4016 error = sbuf_finish(&sbuf);
4023 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
4025 uma_zone_t zone = *(uma_zone_t *)arg1;
4028 max = uma_zone_get_max(zone);
4029 error = sysctl_handle_int(oidp, &max, 0, req);
4030 if (error || !req->newptr)
4033 uma_zone_set_max(zone, max);
4039 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
4041 uma_zone_t zone = *(uma_zone_t *)arg1;
4044 cur = uma_zone_get_cur(zone);
4045 return (sysctl_handle_int(oidp, &cur, 0, req));
4050 uma_dbg_getslab(uma_zone_t zone, void *item)
4056 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
4057 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
4058 slab = vtoslab((vm_offset_t)mem);
4061 * It is safe to return the slab here even though the
4062 * zone is unlocked because the item's allocation state
4063 * essentially holds a reference.
4066 keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
4067 if (keg->uk_flags & UMA_ZONE_HASH)
4068 slab = hash_sfind(&keg->uk_hash, mem);
4070 slab = (uma_slab_t)(mem + keg->uk_pgoff);
4078 uma_dbg_zskip(uma_zone_t zone, void *mem)
4082 if ((keg = zone_first_keg(zone)) == NULL)
4085 return (uma_dbg_kskip(keg, mem));
4089 uma_dbg_kskip(uma_keg_t keg, void *mem)
4093 if (dbg_divisor == 0)
4096 if (dbg_divisor == 1)
4099 idx = (uintptr_t)mem >> PAGE_SHIFT;
4100 if (keg->uk_ipers > 1) {
4101 idx *= keg->uk_ipers;
4102 idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
4105 if ((idx / dbg_divisor) * dbg_divisor != idx) {
4106 counter_u64_add(uma_skip_cnt, 1);
4109 counter_u64_add(uma_dbg_cnt, 1);
4115 * Set up the slab's freei data such that uma_dbg_free can function.
4119 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
4125 slab = uma_dbg_getslab(zone, item);
4127 panic("uma: item %p did not belong to zone %s\n",
4128 item, zone->uz_name);
4131 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4133 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4134 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
4135 item, zone, zone->uz_name, slab, freei);
4136 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4142 * Verifies freed addresses. Checks for alignment, valid slab membership
4143 * and duplicate frees.
4147 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
4153 slab = uma_dbg_getslab(zone, item);
4155 panic("uma: Freed item %p did not belong to zone %s\n",
4156 item, zone->uz_name);
4159 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4161 if (freei >= keg->uk_ipers)
4162 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
4163 item, zone, zone->uz_name, slab, freei);
4165 if (((freei * keg->uk_rsize) + slab->us_data) != item)
4166 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
4167 item, zone, zone->uz_name, slab, freei);
4169 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4170 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
4171 item, zone, zone->uz_name, slab, freei);
4173 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4175 #endif /* INVARIANTS */
4178 DB_SHOW_COMMAND(uma, db_show_uma)
4180 uma_bucket_t bucket;
4183 uma_zone_domain_t zdom;
4184 uint64_t allocs, frees, sleeps;
4187 db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
4188 "Free", "Requests", "Sleeps", "Bucket");
4189 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4190 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4191 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
4192 allocs = z->uz_allocs;
4193 frees = z->uz_frees;
4194 sleeps = z->uz_sleeps;
4197 uma_zone_sumstat(z, &cachefree, &allocs,
4199 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
4200 (LIST_FIRST(&kz->uk_zones) != z)))
4201 cachefree += kz->uk_free;
4202 for (i = 0; i < vm_ndomains; i++) {
4203 zdom = &z->uz_domain[i];
4204 LIST_FOREACH(bucket, &zdom->uzd_buckets,
4206 cachefree += bucket->ub_cnt;
4208 db_printf("%18s %8ju %8jd %8d %12ju %8ju %8u\n",
4209 z->uz_name, (uintmax_t)kz->uk_size,
4210 (intmax_t)(allocs - frees), cachefree,
4211 (uintmax_t)allocs, sleeps, z->uz_count);
4218 DB_SHOW_COMMAND(umacache, db_show_umacache)
4220 uma_bucket_t bucket;
4222 uma_zone_domain_t zdom;
4223 uint64_t allocs, frees;
4226 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
4227 "Requests", "Bucket");
4228 LIST_FOREACH(z, &uma_cachezones, uz_link) {
4229 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
4230 for (i = 0; i < vm_ndomains; i++) {
4231 zdom = &z->uz_domain[i];
4232 LIST_FOREACH(bucket, &zdom->uzd_buckets, ub_link)
4233 cachefree += bucket->ub_cnt;
4235 db_printf("%18s %8ju %8jd %8d %12ju %8u\n",
4236 z->uz_name, (uintmax_t)z->uz_size,
4237 (intmax_t)(allocs - frees), cachefree,
4238 (uintmax_t)allocs, z->uz_count);