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/domainset.h>
63 #include <sys/eventhandler.h>
64 #include <sys/kernel.h>
65 #include <sys/types.h>
66 #include <sys/limits.h>
67 #include <sys/queue.h>
68 #include <sys/malloc.h>
71 #include <sys/sysctl.h>
72 #include <sys/mutex.h>
74 #include <sys/random.h>
75 #include <sys/rwlock.h>
77 #include <sys/sched.h>
79 #include <sys/taskqueue.h>
80 #include <sys/vmmeter.h>
83 #include <vm/vm_domainset.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_pageout.h>
87 #include <vm/vm_param.h>
88 #include <vm/vm_phys.h>
89 #include <vm/vm_pagequeue.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_extern.h>
94 #include <vm/uma_int.h>
95 #include <vm/uma_dbg.h>
100 #include <vm/memguard.h>
104 * This is the zone and keg from which all zones are spawned.
106 static uma_zone_t kegs;
107 static uma_zone_t zones;
109 /* This is the zone from which all offpage uma_slab_ts are allocated. */
110 static uma_zone_t slabzone;
113 * The initial hash tables come out of this zone so they can be allocated
114 * prior to malloc coming up.
116 static uma_zone_t hashzone;
118 /* The boot-time adjusted value for cache line alignment. */
119 int uma_align_cache = 64 - 1;
121 static MALLOC_DEFINE(M_UMAHASH, "UMAHash", "UMA Hash Buckets");
124 * Are we allowed to allocate buckets?
126 static int bucketdisable = 1;
128 /* Linked list of all kegs in the system */
129 static LIST_HEAD(,uma_keg) uma_kegs = LIST_HEAD_INITIALIZER(uma_kegs);
131 /* Linked list of all cache-only zones in the system */
132 static LIST_HEAD(,uma_zone) uma_cachezones =
133 LIST_HEAD_INITIALIZER(uma_cachezones);
135 /* This RW lock protects the keg list */
136 static struct rwlock_padalign __exclusive_cache_line uma_rwlock;
139 * Pointer and counter to pool of pages, that is preallocated at
140 * startup to bootstrap UMA.
142 static char *bootmem;
143 static int boot_pages;
145 static struct sx uma_drain_lock;
147 /* kmem soft limit. */
148 static unsigned long uma_kmem_limit = LONG_MAX;
149 static volatile unsigned long uma_kmem_total;
151 /* Is the VM done starting up? */
152 static enum { BOOT_COLD = 0, BOOT_STRAPPED, BOOT_PAGEALLOC, BOOT_BUCKETS,
153 BOOT_RUNNING } booted = BOOT_COLD;
156 * This is the handle used to schedule events that need to happen
157 * outside of the allocation fast path.
159 static struct callout uma_callout;
160 #define UMA_TIMEOUT 20 /* Seconds for callout interval. */
163 * This structure is passed as the zone ctor arg so that I don't have to create
164 * a special allocation function just for zones.
166 struct uma_zctor_args {
181 struct uma_kctor_args {
190 struct uma_bucket_zone {
193 int ubz_entries; /* Number of items it can hold. */
194 int ubz_maxsize; /* Maximum allocation size per-item. */
198 * Compute the actual number of bucket entries to pack them in power
199 * of two sizes for more efficient space utilization.
201 #define BUCKET_SIZE(n) \
202 (((sizeof(void *) * (n)) - sizeof(struct uma_bucket)) / sizeof(void *))
204 #define BUCKET_MAX BUCKET_SIZE(256)
206 struct uma_bucket_zone bucket_zones[] = {
207 { NULL, "4 Bucket", BUCKET_SIZE(4), 4096 },
208 { NULL, "6 Bucket", BUCKET_SIZE(6), 3072 },
209 { NULL, "8 Bucket", BUCKET_SIZE(8), 2048 },
210 { NULL, "12 Bucket", BUCKET_SIZE(12), 1536 },
211 { NULL, "16 Bucket", BUCKET_SIZE(16), 1024 },
212 { NULL, "32 Bucket", BUCKET_SIZE(32), 512 },
213 { NULL, "64 Bucket", BUCKET_SIZE(64), 256 },
214 { NULL, "128 Bucket", BUCKET_SIZE(128), 128 },
215 { NULL, "256 Bucket", BUCKET_SIZE(256), 64 },
220 * Flags and enumerations to be passed to internal functions.
224 SKIP_CNT = 0x00000001,
225 SKIP_DTOR = 0x00010000,
226 SKIP_FINI = 0x00020000,
229 #define UMA_ANYDOMAIN -1 /* Special value for domain search. */
233 int uma_startup_count(int);
234 void uma_startup(void *, int);
235 void uma_startup1(void);
236 void uma_startup2(void);
238 static void *noobj_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
239 static void *page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
240 static void *pcpu_page_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
241 static void *startup_alloc(uma_zone_t, vm_size_t, int, uint8_t *, int);
242 static void page_free(void *, vm_size_t, uint8_t);
243 static void pcpu_page_free(void *, vm_size_t, uint8_t);
244 static uma_slab_t keg_alloc_slab(uma_keg_t, uma_zone_t, int, int, int);
245 static void cache_drain(uma_zone_t);
246 static void bucket_drain(uma_zone_t, uma_bucket_t);
247 static void bucket_cache_drain(uma_zone_t zone);
248 static int keg_ctor(void *, int, void *, int);
249 static void keg_dtor(void *, int, void *);
250 static int zone_ctor(void *, int, void *, int);
251 static void zone_dtor(void *, int, void *);
252 static int zero_init(void *, int, int);
253 static void keg_small_init(uma_keg_t keg);
254 static void keg_large_init(uma_keg_t keg);
255 static void zone_foreach(void (*zfunc)(uma_zone_t));
256 static void zone_timeout(uma_zone_t zone);
257 static int hash_alloc(struct uma_hash *);
258 static int hash_expand(struct uma_hash *, struct uma_hash *);
259 static void hash_free(struct uma_hash *hash);
260 static void uma_timeout(void *);
261 static void uma_startup3(void);
262 static void *zone_alloc_item(uma_zone_t, void *, int, int);
263 static void *zone_alloc_item_locked(uma_zone_t, void *, int, int);
264 static void zone_free_item(uma_zone_t, void *, void *, enum zfreeskip);
265 static void bucket_enable(void);
266 static void bucket_init(void);
267 static uma_bucket_t bucket_alloc(uma_zone_t zone, void *, int);
268 static void bucket_free(uma_zone_t zone, uma_bucket_t, void *);
269 static void bucket_zone_drain(void);
270 static uma_bucket_t zone_alloc_bucket(uma_zone_t, void *, int, int, int);
271 static uma_slab_t zone_fetch_slab(uma_zone_t, uma_keg_t, int, int);
272 static void *slab_alloc_item(uma_keg_t keg, uma_slab_t slab);
273 static void slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item);
274 static uma_keg_t uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit,
275 uma_fini fini, int align, uint32_t flags);
276 static int zone_import(uma_zone_t, void **, int, int, int);
277 static void zone_release(uma_zone_t, void **, int);
278 static void uma_zero_item(void *, uma_zone_t);
280 void uma_print_zone(uma_zone_t);
281 void uma_print_stats(void);
282 static int sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS);
283 static int sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS);
286 static bool uma_dbg_kskip(uma_keg_t keg, void *mem);
287 static bool uma_dbg_zskip(uma_zone_t zone, void *mem);
288 static void uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item);
289 static void uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item);
291 static SYSCTL_NODE(_vm, OID_AUTO, debug, CTLFLAG_RD, 0,
292 "Memory allocation debugging");
294 static u_int dbg_divisor = 1;
295 SYSCTL_UINT(_vm_debug, OID_AUTO, divisor,
296 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &dbg_divisor, 0,
297 "Debug & thrash every this item in memory allocator");
299 static counter_u64_t uma_dbg_cnt = EARLY_COUNTER;
300 static counter_u64_t uma_skip_cnt = EARLY_COUNTER;
301 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, trashed, CTLFLAG_RD,
302 &uma_dbg_cnt, "memory items debugged");
303 SYSCTL_COUNTER_U64(_vm_debug, OID_AUTO, skipped, CTLFLAG_RD,
304 &uma_skip_cnt, "memory items skipped, not debugged");
307 SYSINIT(uma_startup3, SI_SUB_VM_CONF, SI_ORDER_SECOND, uma_startup3, NULL);
309 SYSCTL_PROC(_vm, OID_AUTO, zone_count, CTLFLAG_RD|CTLTYPE_INT,
310 0, 0, sysctl_vm_zone_count, "I", "Number of UMA zones");
312 SYSCTL_PROC(_vm, OID_AUTO, zone_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
313 0, 0, sysctl_vm_zone_stats, "s,struct uma_type_header", "Zone Stats");
315 static int zone_warnings = 1;
316 SYSCTL_INT(_vm, OID_AUTO, zone_warnings, CTLFLAG_RWTUN, &zone_warnings, 0,
317 "Warn when UMA zones becomes full");
319 /* Adjust bytes under management by UMA. */
321 uma_total_dec(unsigned long size)
324 atomic_subtract_long(&uma_kmem_total, size);
328 uma_total_inc(unsigned long size)
331 if (atomic_fetchadd_long(&uma_kmem_total, size) > uma_kmem_limit)
332 uma_reclaim_wakeup();
336 * This routine checks to see whether or not it's safe to enable buckets.
341 bucketdisable = vm_page_count_min();
345 * Initialize bucket_zones, the array of zones of buckets of various sizes.
347 * For each zone, calculate the memory required for each bucket, consisting
348 * of the header and an array of pointers.
353 struct uma_bucket_zone *ubz;
356 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++) {
357 size = roundup(sizeof(struct uma_bucket), sizeof(void *));
358 size += sizeof(void *) * ubz->ubz_entries;
359 ubz->ubz_zone = uma_zcreate(ubz->ubz_name, size,
360 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
361 UMA_ZONE_MTXCLASS | UMA_ZFLAG_BUCKET | UMA_ZONE_NUMA);
366 * Given a desired number of entries for a bucket, return the zone from which
367 * to allocate the bucket.
369 static struct uma_bucket_zone *
370 bucket_zone_lookup(int entries)
372 struct uma_bucket_zone *ubz;
374 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
375 if (ubz->ubz_entries >= entries)
382 bucket_select(int size)
384 struct uma_bucket_zone *ubz;
386 ubz = &bucket_zones[0];
387 if (size > ubz->ubz_maxsize)
388 return MAX((ubz->ubz_maxsize * ubz->ubz_entries) / size, 1);
390 for (; ubz->ubz_entries != 0; ubz++)
391 if (ubz->ubz_maxsize < size)
394 return (ubz->ubz_entries);
398 bucket_alloc(uma_zone_t zone, void *udata, int flags)
400 struct uma_bucket_zone *ubz;
404 * This is to stop us from allocating per cpu buckets while we're
405 * running out of vm.boot_pages. Otherwise, we would exhaust the
406 * boot pages. This also prevents us from allocating buckets in
407 * low memory situations.
412 * To limit bucket recursion we store the original zone flags
413 * in a cookie passed via zalloc_arg/zfree_arg. This allows the
414 * NOVM flag to persist even through deep recursions. We also
415 * store ZFLAG_BUCKET once we have recursed attempting to allocate
416 * a bucket for a bucket zone so we do not allow infinite bucket
417 * recursion. This cookie will even persist to frees of unused
418 * buckets via the allocation path or bucket allocations in the
421 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
422 udata = (void *)(uintptr_t)zone->uz_flags;
424 if ((uintptr_t)udata & UMA_ZFLAG_BUCKET)
426 udata = (void *)((uintptr_t)udata | UMA_ZFLAG_BUCKET);
428 if ((uintptr_t)udata & UMA_ZFLAG_CACHEONLY)
430 ubz = bucket_zone_lookup(zone->uz_count);
431 if (ubz->ubz_zone == zone && (ubz + 1)->ubz_entries != 0)
433 bucket = uma_zalloc_arg(ubz->ubz_zone, udata, flags);
436 bzero(bucket->ub_bucket, sizeof(void *) * ubz->ubz_entries);
439 bucket->ub_entries = ubz->ubz_entries;
446 bucket_free(uma_zone_t zone, uma_bucket_t bucket, void *udata)
448 struct uma_bucket_zone *ubz;
450 KASSERT(bucket->ub_cnt == 0,
451 ("bucket_free: Freeing a non free bucket."));
452 if ((zone->uz_flags & UMA_ZFLAG_BUCKET) == 0)
453 udata = (void *)(uintptr_t)zone->uz_flags;
454 ubz = bucket_zone_lookup(bucket->ub_entries);
455 uma_zfree_arg(ubz->ubz_zone, bucket, udata);
459 bucket_zone_drain(void)
461 struct uma_bucket_zone *ubz;
463 for (ubz = &bucket_zones[0]; ubz->ubz_entries != 0; ubz++)
464 zone_drain(ubz->ubz_zone);
468 zone_try_fetch_bucket(uma_zone_t zone, uma_zone_domain_t zdom, const bool ws)
472 ZONE_LOCK_ASSERT(zone);
474 if ((bucket = LIST_FIRST(&zdom->uzd_buckets)) != NULL) {
475 MPASS(zdom->uzd_nitems >= bucket->ub_cnt);
476 LIST_REMOVE(bucket, ub_link);
477 zdom->uzd_nitems -= bucket->ub_cnt;
478 if (ws && zdom->uzd_imin > zdom->uzd_nitems)
479 zdom->uzd_imin = zdom->uzd_nitems;
480 zone->uz_bkt_count -= bucket->ub_cnt;
486 zone_put_bucket(uma_zone_t zone, uma_zone_domain_t zdom, uma_bucket_t bucket,
490 ZONE_LOCK_ASSERT(zone);
491 KASSERT(zone->uz_bkt_count < zone->uz_bkt_max, ("%s: zone %p overflow",
494 LIST_INSERT_HEAD(&zdom->uzd_buckets, bucket, ub_link);
495 zdom->uzd_nitems += bucket->ub_cnt;
496 if (ws && zdom->uzd_imax < zdom->uzd_nitems)
497 zdom->uzd_imax = zdom->uzd_nitems;
498 zone->uz_bkt_count += bucket->ub_cnt;
502 zone_log_warning(uma_zone_t zone)
504 static const struct timeval warninterval = { 300, 0 };
506 if (!zone_warnings || zone->uz_warning == NULL)
509 if (ratecheck(&zone->uz_ratecheck, &warninterval))
510 printf("[zone: %s] %s\n", zone->uz_name, zone->uz_warning);
514 zone_maxaction(uma_zone_t zone)
517 if (zone->uz_maxaction.ta_func != NULL)
518 taskqueue_enqueue(taskqueue_thread, &zone->uz_maxaction);
522 * Routine called by timeout which is used to fire off some time interval
523 * based calculations. (stats, hash size, etc.)
532 uma_timeout(void *unused)
535 zone_foreach(zone_timeout);
537 /* Reschedule this event */
538 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
542 * Update the working set size estimate for the zone's bucket cache.
543 * The constants chosen here are somewhat arbitrary. With an update period of
544 * 20s (UMA_TIMEOUT), this estimate is dominated by zone activity over the
548 zone_domain_update_wss(uma_zone_domain_t zdom)
552 MPASS(zdom->uzd_imax >= zdom->uzd_imin);
553 wss = zdom->uzd_imax - zdom->uzd_imin;
554 zdom->uzd_imax = zdom->uzd_imin = zdom->uzd_nitems;
555 zdom->uzd_wss = (3 * wss + 2 * zdom->uzd_wss) / 5;
559 * Routine to perform timeout driven calculations. This expands the
560 * hashes and does per cpu statistics aggregation.
565 zone_timeout(uma_zone_t zone)
567 uma_keg_t keg = zone->uz_keg;
571 * Expand the keg hash table.
573 * This is done if the number of slabs is larger than the hash size.
574 * What I'm trying to do here is completely reduce collisions. This
575 * may be a little aggressive. Should I allow for two collisions max?
577 if (keg->uk_flags & UMA_ZONE_HASH &&
578 keg->uk_pages / keg->uk_ppera >= keg->uk_hash.uh_hashsize) {
579 struct uma_hash newhash;
580 struct uma_hash oldhash;
584 * This is so involved because allocating and freeing
585 * while the keg lock is held will lead to deadlock.
586 * I have to do everything in stages and check for
589 newhash = keg->uk_hash;
591 ret = hash_alloc(&newhash);
594 if (hash_expand(&keg->uk_hash, &newhash)) {
595 oldhash = keg->uk_hash;
596 keg->uk_hash = newhash;
606 for (int i = 0; i < vm_ndomains; i++)
607 zone_domain_update_wss(&zone->uz_domain[i]);
613 * Allocate and zero fill the next sized hash table from the appropriate
617 * hash A new hash structure with the old hash size in uh_hashsize
620 * 1 on success and 0 on failure.
623 hash_alloc(struct uma_hash *hash)
628 oldsize = hash->uh_hashsize;
630 /* We're just going to go to a power of two greater */
632 hash->uh_hashsize = oldsize * 2;
633 alloc = sizeof(hash->uh_slab_hash[0]) * hash->uh_hashsize;
634 hash->uh_slab_hash = (struct slabhead *)malloc(alloc,
635 M_UMAHASH, M_NOWAIT);
637 alloc = sizeof(hash->uh_slab_hash[0]) * UMA_HASH_SIZE_INIT;
638 hash->uh_slab_hash = zone_alloc_item(hashzone, NULL,
639 UMA_ANYDOMAIN, M_WAITOK);
640 hash->uh_hashsize = UMA_HASH_SIZE_INIT;
642 if (hash->uh_slab_hash) {
643 bzero(hash->uh_slab_hash, alloc);
644 hash->uh_hashmask = hash->uh_hashsize - 1;
652 * Expands the hash table for HASH zones. This is done from zone_timeout
653 * to reduce collisions. This must not be done in the regular allocation
654 * path, otherwise, we can recurse on the vm while allocating pages.
657 * oldhash The hash you want to expand
658 * newhash The hash structure for the new table
666 hash_expand(struct uma_hash *oldhash, struct uma_hash *newhash)
672 if (!newhash->uh_slab_hash)
675 if (oldhash->uh_hashsize >= newhash->uh_hashsize)
679 * I need to investigate hash algorithms for resizing without a
683 for (i = 0; i < oldhash->uh_hashsize; i++)
684 while (!SLIST_EMPTY(&oldhash->uh_slab_hash[i])) {
685 slab = SLIST_FIRST(&oldhash->uh_slab_hash[i]);
686 SLIST_REMOVE_HEAD(&oldhash->uh_slab_hash[i], us_hlink);
687 hval = UMA_HASH(newhash, slab->us_data);
688 SLIST_INSERT_HEAD(&newhash->uh_slab_hash[hval],
696 * Free the hash bucket to the appropriate backing store.
699 * slab_hash The hash bucket we're freeing
700 * hashsize The number of entries in that hash bucket
706 hash_free(struct uma_hash *hash)
708 if (hash->uh_slab_hash == NULL)
710 if (hash->uh_hashsize == UMA_HASH_SIZE_INIT)
711 zone_free_item(hashzone, hash->uh_slab_hash, NULL, SKIP_NONE);
713 free(hash->uh_slab_hash, M_UMAHASH);
717 * Frees all outstanding items in a bucket
720 * zone The zone to free to, must be unlocked.
721 * bucket The free/alloc bucket with items, cpu queue must be locked.
728 bucket_drain(uma_zone_t zone, uma_bucket_t bucket)
736 for (i = 0; i < bucket->ub_cnt; i++)
737 zone->uz_fini(bucket->ub_bucket[i], zone->uz_size);
738 zone->uz_release(zone->uz_arg, bucket->ub_bucket, bucket->ub_cnt);
739 if (zone->uz_max_items > 0) {
741 zone->uz_items -= bucket->ub_cnt;
742 if (zone->uz_sleepers && zone->uz_items < zone->uz_max_items)
750 * Drains the per cpu caches for a zone.
752 * NOTE: This may only be called while the zone is being turn down, and not
753 * during normal operation. This is necessary in order that we do not have
754 * to migrate CPUs to drain the per-CPU caches.
757 * zone The zone to drain, must be unlocked.
763 cache_drain(uma_zone_t zone)
769 * XXX: It is safe to not lock the per-CPU caches, because we're
770 * tearing down the zone anyway. I.e., there will be no further use
771 * of the caches at this point.
773 * XXX: It would good to be able to assert that the zone is being
774 * torn down to prevent improper use of cache_drain().
776 * XXX: We lock the zone before passing into bucket_cache_drain() as
777 * it is used elsewhere. Should the tear-down path be made special
778 * there in some form?
781 cache = &zone->uz_cpu[cpu];
782 bucket_drain(zone, cache->uc_allocbucket);
783 bucket_drain(zone, cache->uc_freebucket);
784 if (cache->uc_allocbucket != NULL)
785 bucket_free(zone, cache->uc_allocbucket, NULL);
786 if (cache->uc_freebucket != NULL)
787 bucket_free(zone, cache->uc_freebucket, NULL);
788 cache->uc_allocbucket = cache->uc_freebucket = NULL;
791 bucket_cache_drain(zone);
796 cache_shrink(uma_zone_t zone)
799 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
803 zone->uz_count = (zone->uz_count_min + zone->uz_count) / 2;
808 cache_drain_safe_cpu(uma_zone_t zone)
814 if (zone->uz_flags & UMA_ZFLAG_INTERNAL)
820 if (zone->uz_flags & UMA_ZONE_NUMA)
821 domain = PCPU_GET(domain);
824 cache = &zone->uz_cpu[curcpu];
825 if (cache->uc_allocbucket) {
826 if (cache->uc_allocbucket->ub_cnt != 0)
827 zone_put_bucket(zone, &zone->uz_domain[domain],
828 cache->uc_allocbucket, false);
830 b1 = cache->uc_allocbucket;
831 cache->uc_allocbucket = NULL;
833 if (cache->uc_freebucket) {
834 if (cache->uc_freebucket->ub_cnt != 0)
835 zone_put_bucket(zone, &zone->uz_domain[domain],
836 cache->uc_freebucket, false);
838 b2 = cache->uc_freebucket;
839 cache->uc_freebucket = NULL;
844 bucket_free(zone, b1, NULL);
846 bucket_free(zone, b2, NULL);
850 * Safely drain per-CPU caches of a zone(s) to alloc bucket.
851 * This is an expensive call because it needs to bind to all CPUs
852 * one by one and enter a critical section on each of them in order
853 * to safely access their cache buckets.
854 * Zone lock must not be held on call this function.
857 cache_drain_safe(uma_zone_t zone)
862 * Polite bucket sizes shrinking was not enouth, shrink aggressively.
867 zone_foreach(cache_shrink);
870 thread_lock(curthread);
871 sched_bind(curthread, cpu);
872 thread_unlock(curthread);
875 cache_drain_safe_cpu(zone);
877 zone_foreach(cache_drain_safe_cpu);
879 thread_lock(curthread);
880 sched_unbind(curthread);
881 thread_unlock(curthread);
885 * Drain the cached buckets from a zone. Expects a locked zone on entry.
888 bucket_cache_drain(uma_zone_t zone)
890 uma_zone_domain_t zdom;
895 * Drain the bucket queues and free the buckets.
897 for (i = 0; i < vm_ndomains; i++) {
898 zdom = &zone->uz_domain[i];
899 while ((bucket = zone_try_fetch_bucket(zone, zdom, false)) !=
902 bucket_drain(zone, bucket);
903 bucket_free(zone, bucket, NULL);
909 * Shrink further bucket sizes. Price of single zone lock collision
910 * is probably lower then price of global cache drain.
912 if (zone->uz_count > zone->uz_count_min)
917 keg_free_slab(uma_keg_t keg, uma_slab_t slab, int start)
923 CTR4(KTR_UMA, "keg_free_slab keg %s(%p) slab %p, returning %d bytes",
924 keg->uk_name, keg, slab, PAGE_SIZE * keg->uk_ppera);
927 flags = slab->us_flags;
929 if (keg->uk_fini != NULL) {
930 for (i--; i > -1; i--)
933 * trash_fini implies that dtor was trash_dtor. trash_fini
934 * would check that memory hasn't been modified since free,
935 * which executed trash_dtor.
936 * That's why we need to run uma_dbg_kskip() check here,
937 * albeit we don't make skip check for other init/fini
940 if (!uma_dbg_kskip(keg, slab->us_data + (keg->uk_rsize * i)) ||
941 keg->uk_fini != trash_fini)
943 keg->uk_fini(slab->us_data + (keg->uk_rsize * i),
946 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
947 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
948 keg->uk_freef(mem, PAGE_SIZE * keg->uk_ppera, flags);
949 uma_total_dec(PAGE_SIZE * keg->uk_ppera);
953 * Frees pages from a keg back to the system. This is done on demand from
954 * the pageout daemon.
959 keg_drain(uma_keg_t keg)
961 struct slabhead freeslabs = { 0 };
963 uma_slab_t slab, tmp;
967 * We don't want to take pages from statically allocated kegs at this
970 if (keg->uk_flags & UMA_ZONE_NOFREE || keg->uk_freef == NULL)
973 CTR3(KTR_UMA, "keg_drain %s(%p) free items: %u",
974 keg->uk_name, keg, keg->uk_free);
976 if (keg->uk_free == 0)
979 for (i = 0; i < vm_ndomains; i++) {
980 dom = &keg->uk_domain[i];
981 LIST_FOREACH_SAFE(slab, &dom->ud_free_slab, us_link, tmp) {
982 /* We have nowhere to free these to. */
983 if (slab->us_flags & UMA_SLAB_BOOT)
986 LIST_REMOVE(slab, us_link);
987 keg->uk_pages -= keg->uk_ppera;
988 keg->uk_free -= keg->uk_ipers;
990 if (keg->uk_flags & UMA_ZONE_HASH)
991 UMA_HASH_REMOVE(&keg->uk_hash, slab,
994 SLIST_INSERT_HEAD(&freeslabs, slab, us_hlink);
1001 while ((slab = SLIST_FIRST(&freeslabs)) != NULL) {
1002 SLIST_REMOVE(&freeslabs, slab, uma_slab, us_hlink);
1003 keg_free_slab(keg, slab, keg->uk_ipers);
1008 zone_drain_wait(uma_zone_t zone, int waitok)
1012 * Set draining to interlock with zone_dtor() so we can release our
1013 * locks as we go. Only dtor() should do a WAITOK call since it
1014 * is the only call that knows the structure will still be available
1018 while (zone->uz_flags & UMA_ZFLAG_DRAINING) {
1019 if (waitok == M_NOWAIT)
1021 msleep(zone, zone->uz_lockptr, PVM, "zonedrain", 1);
1023 zone->uz_flags |= UMA_ZFLAG_DRAINING;
1024 bucket_cache_drain(zone);
1027 * The DRAINING flag protects us from being freed while
1028 * we're running. Normally the uma_rwlock would protect us but we
1029 * must be able to release and acquire the right lock for each keg.
1031 keg_drain(zone->uz_keg);
1033 zone->uz_flags &= ~UMA_ZFLAG_DRAINING;
1040 zone_drain(uma_zone_t zone)
1043 zone_drain_wait(zone, M_NOWAIT);
1047 * Allocate a new slab for a keg. This does not insert the slab onto a list.
1048 * If the allocation was successful, the keg lock will be held upon return,
1049 * otherwise the keg will be left unlocked.
1052 * flags Wait flags for the item initialization routine
1053 * aflags Wait flags for the slab allocation
1056 * The slab that was allocated or NULL if there is no memory and the
1057 * caller specified M_NOWAIT.
1060 keg_alloc_slab(uma_keg_t keg, uma_zone_t zone, int domain, int flags,
1070 KASSERT(domain >= 0 && domain < vm_ndomains,
1071 ("keg_alloc_slab: domain %d out of range", domain));
1072 KEG_LOCK_ASSERT(keg);
1073 MPASS(zone->uz_lockptr == &keg->uk_lock);
1075 allocf = keg->uk_allocf;
1080 if (keg->uk_flags & UMA_ZONE_OFFPAGE) {
1081 slab = zone_alloc_item(keg->uk_slabzone, NULL, domain, aflags);
1087 * This reproduces the old vm_zone behavior of zero filling pages the
1088 * first time they are added to a zone.
1090 * Malloced items are zeroed in uma_zalloc.
1093 if ((keg->uk_flags & UMA_ZONE_MALLOC) == 0)
1098 if (keg->uk_flags & UMA_ZONE_NODUMP)
1101 /* zone is passed for legacy reasons. */
1102 size = keg->uk_ppera * PAGE_SIZE;
1103 mem = allocf(zone, size, domain, &sflags, aflags);
1105 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1106 zone_free_item(keg->uk_slabzone, slab, NULL, SKIP_NONE);
1110 uma_total_inc(size);
1112 /* Point the slab into the allocated memory */
1113 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE))
1114 slab = (uma_slab_t )(mem + keg->uk_pgoff);
1116 if (keg->uk_flags & UMA_ZONE_VTOSLAB)
1117 for (i = 0; i < keg->uk_ppera; i++)
1118 vsetslab((vm_offset_t)mem + (i * PAGE_SIZE), slab);
1121 slab->us_data = mem;
1122 slab->us_freecount = keg->uk_ipers;
1123 slab->us_flags = sflags;
1124 slab->us_domain = domain;
1125 BIT_FILL(SLAB_SETSIZE, &slab->us_free);
1127 BIT_ZERO(SLAB_SETSIZE, &slab->us_debugfree);
1130 if (keg->uk_init != NULL) {
1131 for (i = 0; i < keg->uk_ipers; i++)
1132 if (keg->uk_init(slab->us_data + (keg->uk_rsize * i),
1133 keg->uk_size, flags) != 0)
1135 if (i != keg->uk_ipers) {
1136 keg_free_slab(keg, slab, i);
1143 CTR3(KTR_UMA, "keg_alloc_slab: allocated slab %p for %s(%p)",
1144 slab, keg->uk_name, keg);
1146 if (keg->uk_flags & UMA_ZONE_HASH)
1147 UMA_HASH_INSERT(&keg->uk_hash, slab, mem);
1149 keg->uk_pages += keg->uk_ppera;
1150 keg->uk_free += keg->uk_ipers;
1157 * This function is intended to be used early on in place of page_alloc() so
1158 * that we may use the boot time page cache to satisfy allocations before
1162 startup_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1171 * If we are in BOOT_BUCKETS or higher, than switch to real
1172 * allocator. Zones with page sized slabs switch at BOOT_PAGEALLOC.
1178 case BOOT_PAGEALLOC:
1179 if (keg->uk_ppera > 1)
1183 #ifdef UMA_MD_SMALL_ALLOC
1184 keg->uk_allocf = (keg->uk_ppera > 1) ?
1185 page_alloc : uma_small_alloc;
1187 keg->uk_allocf = page_alloc;
1189 return keg->uk_allocf(zone, bytes, domain, pflag, wait);
1193 * Check our small startup cache to see if it has pages remaining.
1195 pages = howmany(bytes, PAGE_SIZE);
1196 KASSERT(pages > 0, ("%s can't reserve 0 pages", __func__));
1197 if (pages > boot_pages)
1198 panic("UMA zone \"%s\": Increase vm.boot_pages", zone->uz_name);
1200 printf("%s from \"%s\", %d boot pages left\n", __func__, zone->uz_name,
1204 boot_pages -= pages;
1205 bootmem += pages * PAGE_SIZE;
1206 *pflag = UMA_SLAB_BOOT;
1212 * Allocates a number of pages from the system
1215 * bytes The number of bytes requested
1216 * wait Shall we wait?
1219 * A pointer to the alloced memory or possibly
1220 * NULL if M_NOWAIT is set.
1223 page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1226 void *p; /* Returned page */
1228 *pflag = UMA_SLAB_KERNEL;
1229 p = (void *)kmem_malloc_domainset(DOMAINSET_FIXED(domain), bytes, wait);
1235 pcpu_page_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag,
1238 struct pglist alloctail;
1239 vm_offset_t addr, zkva;
1241 vm_page_t p, p_next;
1246 MPASS(bytes == (mp_maxid + 1) * PAGE_SIZE);
1248 TAILQ_INIT(&alloctail);
1249 flags = VM_ALLOC_SYSTEM | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1250 malloc2vm_flags(wait);
1251 *pflag = UMA_SLAB_KERNEL;
1252 for (cpu = 0; cpu <= mp_maxid; cpu++) {
1253 if (CPU_ABSENT(cpu)) {
1254 p = vm_page_alloc(NULL, 0, flags);
1257 p = vm_page_alloc(NULL, 0, flags);
1259 pc = pcpu_find(cpu);
1260 p = vm_page_alloc_domain(NULL, 0, pc->pc_domain, flags);
1261 if (__predict_false(p == NULL))
1262 p = vm_page_alloc(NULL, 0, flags);
1265 if (__predict_false(p == NULL))
1267 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1269 if ((addr = kva_alloc(bytes)) == 0)
1272 TAILQ_FOREACH(p, &alloctail, listq) {
1273 pmap_qenter(zkva, &p, 1);
1276 return ((void*)addr);
1278 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1279 vm_page_unwire(p, PQ_NONE);
1286 * Allocates a number of pages from within an object
1289 * bytes The number of bytes requested
1290 * wait Shall we wait?
1293 * A pointer to the alloced memory or possibly
1294 * NULL if M_NOWAIT is set.
1297 noobj_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *flags,
1300 TAILQ_HEAD(, vm_page) alloctail;
1302 vm_offset_t retkva, zkva;
1303 vm_page_t p, p_next;
1306 TAILQ_INIT(&alloctail);
1309 npages = howmany(bytes, PAGE_SIZE);
1310 while (npages > 0) {
1311 p = vm_page_alloc_domain(NULL, 0, domain, VM_ALLOC_INTERRUPT |
1312 VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
1313 ((wait & M_WAITOK) != 0 ? VM_ALLOC_WAITOK :
1317 * Since the page does not belong to an object, its
1320 TAILQ_INSERT_TAIL(&alloctail, p, listq);
1325 * Page allocation failed, free intermediate pages and
1328 TAILQ_FOREACH_SAFE(p, &alloctail, listq, p_next) {
1329 vm_page_unwire(p, PQ_NONE);
1334 *flags = UMA_SLAB_PRIV;
1335 zkva = keg->uk_kva +
1336 atomic_fetchadd_long(&keg->uk_offset, round_page(bytes));
1338 TAILQ_FOREACH(p, &alloctail, listq) {
1339 pmap_qenter(zkva, &p, 1);
1343 return ((void *)retkva);
1347 * Frees a number of pages to the system
1350 * mem A pointer to the memory to be freed
1351 * size The size of the memory being freed
1352 * flags The original p->us_flags field
1358 page_free(void *mem, vm_size_t size, uint8_t flags)
1361 if ((flags & UMA_SLAB_KERNEL) == 0)
1362 panic("UMA: page_free used with invalid flags %x", flags);
1364 kmem_free((vm_offset_t)mem, size);
1368 * Frees pcpu zone allocations
1371 * mem A pointer to the memory to be freed
1372 * size The size of the memory being freed
1373 * flags The original p->us_flags field
1379 pcpu_page_free(void *mem, vm_size_t size, uint8_t flags)
1381 vm_offset_t sva, curva;
1385 MPASS(size == (mp_maxid+1)*PAGE_SIZE);
1386 sva = (vm_offset_t)mem;
1387 for (curva = sva; curva < sva + size; curva += PAGE_SIZE) {
1388 paddr = pmap_kextract(curva);
1389 m = PHYS_TO_VM_PAGE(paddr);
1390 vm_page_unwire(m, PQ_NONE);
1393 pmap_qremove(sva, size >> PAGE_SHIFT);
1394 kva_free(sva, size);
1399 * Zero fill initializer
1401 * Arguments/Returns follow uma_init specifications
1404 zero_init(void *mem, int size, int flags)
1411 * Finish creating a small uma keg. This calculates ipers, and the keg size.
1414 * keg The zone we should initialize
1420 keg_small_init(uma_keg_t keg)
1428 if (keg->uk_flags & UMA_ZONE_PCPU) {
1429 u_int ncpus = (mp_maxid + 1) ? (mp_maxid + 1) : MAXCPU;
1431 slabsize = UMA_PCPU_ALLOC_SIZE;
1432 keg->uk_ppera = ncpus;
1434 slabsize = UMA_SLAB_SIZE;
1439 * Calculate the size of each allocation (rsize) according to
1440 * alignment. If the requested size is smaller than we have
1441 * allocation bits for we round it up.
1443 rsize = keg->uk_size;
1444 if (rsize < slabsize / SLAB_SETSIZE)
1445 rsize = slabsize / SLAB_SETSIZE;
1446 if (rsize & keg->uk_align)
1447 rsize = (rsize & ~keg->uk_align) + (keg->uk_align + 1);
1448 keg->uk_rsize = rsize;
1450 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0 ||
1451 keg->uk_rsize < UMA_PCPU_ALLOC_SIZE,
1452 ("%s: size %u too large", __func__, keg->uk_rsize));
1454 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1457 shsize = SIZEOF_UMA_SLAB;
1459 if (rsize <= slabsize - shsize)
1460 keg->uk_ipers = (slabsize - shsize) / rsize;
1462 /* Handle special case when we have 1 item per slab, so
1463 * alignment requirement can be relaxed. */
1464 KASSERT(keg->uk_size <= slabsize - shsize,
1465 ("%s: size %u greater than slab", __func__, keg->uk_size));
1468 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1469 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1471 memused = keg->uk_ipers * rsize + shsize;
1472 wastedspace = slabsize - memused;
1475 * We can't do OFFPAGE if we're internal or if we've been
1476 * asked to not go to the VM for buckets. If we do this we
1477 * may end up going to the VM for slabs which we do not
1478 * want to do if we're UMA_ZFLAG_CACHEONLY as a result
1479 * of UMA_ZONE_VM, which clearly forbids it.
1481 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) ||
1482 (keg->uk_flags & UMA_ZFLAG_CACHEONLY))
1486 * See if using an OFFPAGE slab will limit our waste. Only do
1487 * this if it permits more items per-slab.
1489 * XXX We could try growing slabsize to limit max waste as well.
1490 * Historically this was not done because the VM could not
1491 * efficiently handle contiguous allocations.
1493 if ((wastedspace >= slabsize / UMA_MAX_WASTE) &&
1494 (keg->uk_ipers < (slabsize / keg->uk_rsize))) {
1495 keg->uk_ipers = slabsize / keg->uk_rsize;
1496 KASSERT(keg->uk_ipers > 0 && keg->uk_ipers <= SLAB_SETSIZE,
1497 ("%s: keg->uk_ipers %u", __func__, keg->uk_ipers));
1498 CTR6(KTR_UMA, "UMA decided we need offpage slab headers for "
1499 "keg: %s(%p), calculated wastedspace = %d, "
1500 "maximum wasted space allowed = %d, "
1501 "calculated ipers = %d, "
1502 "new wasted space = %d\n", keg->uk_name, keg, wastedspace,
1503 slabsize / UMA_MAX_WASTE, keg->uk_ipers,
1504 slabsize - keg->uk_ipers * keg->uk_rsize);
1505 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1508 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1509 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1510 keg->uk_flags |= UMA_ZONE_HASH;
1514 * Finish creating a large (> UMA_SLAB_SIZE) uma kegs. Just give in and do
1515 * OFFPAGE for now. When I can allow for more dynamic slab sizes this will be
1519 * keg The keg we should initialize
1525 keg_large_init(uma_keg_t keg)
1528 KASSERT(keg != NULL, ("Keg is null in keg_large_init"));
1529 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1530 ("%s: Cannot large-init a UMA_ZONE_PCPU keg", __func__));
1532 keg->uk_ppera = howmany(keg->uk_size, PAGE_SIZE);
1534 keg->uk_rsize = keg->uk_size;
1536 /* Check whether we have enough space to not do OFFPAGE. */
1537 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) == 0 &&
1538 PAGE_SIZE * keg->uk_ppera - keg->uk_rsize < SIZEOF_UMA_SLAB) {
1540 * We can't do OFFPAGE if we're internal, in which case
1541 * we need an extra page per allocation to contain the
1544 if ((keg->uk_flags & UMA_ZFLAG_INTERNAL) == 0)
1545 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1550 if ((keg->uk_flags & UMA_ZONE_OFFPAGE) &&
1551 (keg->uk_flags & UMA_ZONE_VTOSLAB) == 0)
1552 keg->uk_flags |= UMA_ZONE_HASH;
1556 keg_cachespread_init(uma_keg_t keg)
1563 KASSERT((keg->uk_flags & UMA_ZONE_PCPU) == 0,
1564 ("%s: Cannot cachespread-init a UMA_ZONE_PCPU keg", __func__));
1566 alignsize = keg->uk_align + 1;
1567 rsize = keg->uk_size;
1569 * We want one item to start on every align boundary in a page. To
1570 * do this we will span pages. We will also extend the item by the
1571 * size of align if it is an even multiple of align. Otherwise, it
1572 * would fall on the same boundary every time.
1574 if (rsize & keg->uk_align)
1575 rsize = (rsize & ~keg->uk_align) + alignsize;
1576 if ((rsize & alignsize) == 0)
1578 trailer = rsize - keg->uk_size;
1579 pages = (rsize * (PAGE_SIZE / alignsize)) / PAGE_SIZE;
1580 pages = MIN(pages, (128 * 1024) / PAGE_SIZE);
1581 keg->uk_rsize = rsize;
1582 keg->uk_ppera = pages;
1583 keg->uk_ipers = ((pages * PAGE_SIZE) + trailer) / rsize;
1584 keg->uk_flags |= UMA_ZONE_OFFPAGE | UMA_ZONE_VTOSLAB;
1585 KASSERT(keg->uk_ipers <= SLAB_SETSIZE,
1586 ("%s: keg->uk_ipers too high(%d) increase max_ipers", __func__,
1591 * Keg header ctor. This initializes all fields, locks, etc. And inserts
1592 * the keg onto the global keg list.
1594 * Arguments/Returns follow uma_ctor specifications
1595 * udata Actually uma_kctor_args
1598 keg_ctor(void *mem, int size, void *udata, int flags)
1600 struct uma_kctor_args *arg = udata;
1601 uma_keg_t keg = mem;
1605 keg->uk_size = arg->size;
1606 keg->uk_init = arg->uminit;
1607 keg->uk_fini = arg->fini;
1608 keg->uk_align = arg->align;
1610 keg->uk_reserve = 0;
1612 keg->uk_flags = arg->flags;
1613 keg->uk_slabzone = NULL;
1616 * We use a global round-robin policy by default. Zones with
1617 * UMA_ZONE_NUMA set will use first-touch instead, in which case the
1618 * iterator is never run.
1620 keg->uk_dr.dr_policy = DOMAINSET_RR();
1621 keg->uk_dr.dr_iter = 0;
1624 * The master zone is passed to us at keg-creation time.
1627 keg->uk_name = zone->uz_name;
1629 if (arg->flags & UMA_ZONE_VM)
1630 keg->uk_flags |= UMA_ZFLAG_CACHEONLY;
1632 if (arg->flags & UMA_ZONE_ZINIT)
1633 keg->uk_init = zero_init;
1635 if (arg->flags & UMA_ZONE_MALLOC)
1636 keg->uk_flags |= UMA_ZONE_VTOSLAB;
1638 if (arg->flags & UMA_ZONE_PCPU)
1640 keg->uk_flags |= UMA_ZONE_OFFPAGE;
1642 keg->uk_flags &= ~UMA_ZONE_PCPU;
1645 if (keg->uk_flags & UMA_ZONE_CACHESPREAD) {
1646 keg_cachespread_init(keg);
1648 if (keg->uk_size > UMA_SLAB_SPACE)
1649 keg_large_init(keg);
1651 keg_small_init(keg);
1654 if (keg->uk_flags & UMA_ZONE_OFFPAGE)
1655 keg->uk_slabzone = slabzone;
1658 * If we haven't booted yet we need allocations to go through the
1659 * startup cache until the vm is ready.
1661 if (booted < BOOT_PAGEALLOC)
1662 keg->uk_allocf = startup_alloc;
1663 #ifdef UMA_MD_SMALL_ALLOC
1664 else if (keg->uk_ppera == 1)
1665 keg->uk_allocf = uma_small_alloc;
1667 else if (keg->uk_flags & UMA_ZONE_PCPU)
1668 keg->uk_allocf = pcpu_page_alloc;
1670 keg->uk_allocf = page_alloc;
1671 #ifdef UMA_MD_SMALL_ALLOC
1672 if (keg->uk_ppera == 1)
1673 keg->uk_freef = uma_small_free;
1676 if (keg->uk_flags & UMA_ZONE_PCPU)
1677 keg->uk_freef = pcpu_page_free;
1679 keg->uk_freef = page_free;
1682 * Initialize keg's lock
1684 KEG_LOCK_INIT(keg, (arg->flags & UMA_ZONE_MTXCLASS));
1687 * If we're putting the slab header in the actual page we need to
1688 * figure out where in each page it goes. See SIZEOF_UMA_SLAB
1691 if (!(keg->uk_flags & UMA_ZONE_OFFPAGE)) {
1692 keg->uk_pgoff = (PAGE_SIZE * keg->uk_ppera) - SIZEOF_UMA_SLAB;
1694 * The only way the following is possible is if with our
1695 * UMA_ALIGN_PTR adjustments we are now bigger than
1696 * UMA_SLAB_SIZE. I haven't checked whether this is
1697 * mathematically possible for all cases, so we make
1700 KASSERT(keg->uk_pgoff + sizeof(struct uma_slab) <=
1701 PAGE_SIZE * keg->uk_ppera,
1702 ("zone %s ipers %d rsize %d size %d slab won't fit",
1703 zone->uz_name, keg->uk_ipers, keg->uk_rsize, keg->uk_size));
1706 if (keg->uk_flags & UMA_ZONE_HASH)
1707 hash_alloc(&keg->uk_hash);
1709 CTR5(KTR_UMA, "keg_ctor %p zone %s(%p) out %d free %d\n",
1710 keg, zone->uz_name, zone,
1711 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
1714 LIST_INSERT_HEAD(&keg->uk_zones, zone, uz_link);
1716 rw_wlock(&uma_rwlock);
1717 LIST_INSERT_HEAD(&uma_kegs, keg, uk_link);
1718 rw_wunlock(&uma_rwlock);
1723 zone_alloc_counters(uma_zone_t zone)
1726 zone->uz_allocs = counter_u64_alloc(M_WAITOK);
1727 zone->uz_frees = counter_u64_alloc(M_WAITOK);
1728 zone->uz_fails = counter_u64_alloc(M_WAITOK);
1732 * Zone header ctor. This initializes all fields, locks, etc.
1734 * Arguments/Returns follow uma_ctor specifications
1735 * udata Actually uma_zctor_args
1738 zone_ctor(void *mem, int size, void *udata, int flags)
1740 struct uma_zctor_args *arg = udata;
1741 uma_zone_t zone = mem;
1746 zone->uz_name = arg->name;
1747 zone->uz_ctor = arg->ctor;
1748 zone->uz_dtor = arg->dtor;
1749 zone->uz_slab = zone_fetch_slab;
1750 zone->uz_init = NULL;
1751 zone->uz_fini = NULL;
1752 zone->uz_sleeps = 0;
1754 zone->uz_count_min = 0;
1755 zone->uz_count_max = BUCKET_MAX;
1757 zone->uz_warning = NULL;
1758 /* The domain structures follow the cpu structures. */
1759 zone->uz_domain = (struct uma_zone_domain *)&zone->uz_cpu[mp_ncpus];
1760 zone->uz_bkt_max = ULONG_MAX;
1761 timevalclear(&zone->uz_ratecheck);
1763 if (__predict_true(booted == BOOT_RUNNING))
1764 zone_alloc_counters(zone);
1766 zone->uz_allocs = EARLY_COUNTER;
1767 zone->uz_frees = EARLY_COUNTER;
1768 zone->uz_fails = EARLY_COUNTER;
1772 * This is a pure cache zone, no kegs.
1775 if (arg->flags & UMA_ZONE_VM)
1776 arg->flags |= UMA_ZFLAG_CACHEONLY;
1777 zone->uz_flags = arg->flags;
1778 zone->uz_size = arg->size;
1779 zone->uz_import = arg->import;
1780 zone->uz_release = arg->release;
1781 zone->uz_arg = arg->arg;
1782 zone->uz_lockptr = &zone->uz_lock;
1783 ZONE_LOCK_INIT(zone, (arg->flags & UMA_ZONE_MTXCLASS));
1784 rw_wlock(&uma_rwlock);
1785 LIST_INSERT_HEAD(&uma_cachezones, zone, uz_link);
1786 rw_wunlock(&uma_rwlock);
1791 * Use the regular zone/keg/slab allocator.
1793 zone->uz_import = (uma_import)zone_import;
1794 zone->uz_release = (uma_release)zone_release;
1795 zone->uz_arg = zone;
1798 if (arg->flags & UMA_ZONE_SECONDARY) {
1799 KASSERT(arg->keg != NULL, ("Secondary zone on zero'd keg"));
1800 zone->uz_init = arg->uminit;
1801 zone->uz_fini = arg->fini;
1802 zone->uz_lockptr = &keg->uk_lock;
1803 zone->uz_flags |= UMA_ZONE_SECONDARY;
1804 rw_wlock(&uma_rwlock);
1806 LIST_FOREACH(z, &keg->uk_zones, uz_link) {
1807 if (LIST_NEXT(z, uz_link) == NULL) {
1808 LIST_INSERT_AFTER(z, zone, uz_link);
1813 rw_wunlock(&uma_rwlock);
1814 } else if (keg == NULL) {
1815 if ((keg = uma_kcreate(zone, arg->size, arg->uminit, arg->fini,
1816 arg->align, arg->flags)) == NULL)
1819 struct uma_kctor_args karg;
1822 /* We should only be here from uma_startup() */
1823 karg.size = arg->size;
1824 karg.uminit = arg->uminit;
1825 karg.fini = arg->fini;
1826 karg.align = arg->align;
1827 karg.flags = arg->flags;
1829 error = keg_ctor(arg->keg, sizeof(struct uma_keg), &karg,
1836 zone->uz_size = keg->uk_size;
1837 zone->uz_flags |= (keg->uk_flags &
1838 (UMA_ZONE_INHERIT | UMA_ZFLAG_INHERIT));
1841 * Some internal zones don't have room allocated for the per cpu
1842 * caches. If we're internal, bail out here.
1844 if (keg->uk_flags & UMA_ZFLAG_INTERNAL) {
1845 KASSERT((zone->uz_flags & UMA_ZONE_SECONDARY) == 0,
1846 ("Secondary zone requested UMA_ZFLAG_INTERNAL"));
1851 KASSERT((arg->flags & (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET)) !=
1852 (UMA_ZONE_MAXBUCKET | UMA_ZONE_NOBUCKET),
1853 ("Invalid zone flag combination"));
1854 if ((arg->flags & UMA_ZONE_MAXBUCKET) != 0)
1855 zone->uz_count = BUCKET_MAX;
1856 else if ((arg->flags & UMA_ZONE_NOBUCKET) != 0)
1859 zone->uz_count = bucket_select(zone->uz_size);
1860 zone->uz_count_min = zone->uz_count;
1866 * Keg header dtor. This frees all data, destroys locks, frees the hash
1867 * table and removes the keg from the global list.
1869 * Arguments/Returns follow uma_dtor specifications
1873 keg_dtor(void *arg, int size, void *udata)
1877 keg = (uma_keg_t)arg;
1879 if (keg->uk_free != 0) {
1880 printf("Freed UMA keg (%s) was not empty (%d items). "
1881 " Lost %d pages of memory.\n",
1882 keg->uk_name ? keg->uk_name : "",
1883 keg->uk_free, keg->uk_pages);
1887 hash_free(&keg->uk_hash);
1895 * Arguments/Returns follow uma_dtor specifications
1899 zone_dtor(void *arg, int size, void *udata)
1904 zone = (uma_zone_t)arg;
1906 if (!(zone->uz_flags & UMA_ZFLAG_INTERNAL))
1909 rw_wlock(&uma_rwlock);
1910 LIST_REMOVE(zone, uz_link);
1911 rw_wunlock(&uma_rwlock);
1913 * XXX there are some races here where
1914 * the zone can be drained but zone lock
1915 * released and then refilled before we
1916 * remove it... we dont care for now
1918 zone_drain_wait(zone, M_WAITOK);
1920 * We only destroy kegs from non secondary zones.
1922 if ((keg = zone->uz_keg) != NULL &&
1923 (zone->uz_flags & UMA_ZONE_SECONDARY) == 0) {
1924 rw_wlock(&uma_rwlock);
1925 LIST_REMOVE(keg, uk_link);
1926 rw_wunlock(&uma_rwlock);
1927 zone_free_item(kegs, keg, NULL, SKIP_NONE);
1929 counter_u64_free(zone->uz_allocs);
1930 counter_u64_free(zone->uz_frees);
1931 counter_u64_free(zone->uz_fails);
1932 if (zone->uz_lockptr == &zone->uz_lock)
1933 ZONE_LOCK_FINI(zone);
1937 * Traverses every zone in the system and calls a callback
1940 * zfunc A pointer to a function which accepts a zone
1947 zone_foreach(void (*zfunc)(uma_zone_t))
1953 * Before BOOT_RUNNING we are guaranteed to be single
1954 * threaded, so locking isn't needed. Startup functions
1955 * are allowed to use M_WAITOK.
1957 if (__predict_true(booted == BOOT_RUNNING))
1958 rw_rlock(&uma_rwlock);
1959 LIST_FOREACH(keg, &uma_kegs, uk_link) {
1960 LIST_FOREACH(zone, &keg->uk_zones, uz_link)
1963 if (__predict_true(booted == BOOT_RUNNING))
1964 rw_runlock(&uma_rwlock);
1968 * Count how many pages do we need to bootstrap. VM supplies
1969 * its need in early zones in the argument, we add up our zones,
1970 * which consist of: UMA Slabs, UMA Hash and 9 Bucket zones. The
1971 * zone of zones and zone of kegs are accounted separately.
1973 #define UMA_BOOT_ZONES 11
1974 /* Zone of zones and zone of kegs have arbitrary alignment. */
1975 #define UMA_BOOT_ALIGN 32
1976 static int zsize, ksize;
1978 uma_startup_count(int vm_zones)
1982 ksize = sizeof(struct uma_keg) +
1983 (sizeof(struct uma_domain) * vm_ndomains);
1984 zsize = sizeof(struct uma_zone) +
1985 (sizeof(struct uma_cache) * (mp_maxid + 1)) +
1986 (sizeof(struct uma_zone_domain) * vm_ndomains);
1989 * Memory for the zone of kegs and its keg,
1990 * and for zone of zones.
1992 pages = howmany(roundup(zsize, CACHE_LINE_SIZE) * 2 +
1993 roundup(ksize, CACHE_LINE_SIZE), PAGE_SIZE);
1995 #ifdef UMA_MD_SMALL_ALLOC
1996 zones = UMA_BOOT_ZONES;
1998 zones = UMA_BOOT_ZONES + vm_zones;
2002 /* Memory for the rest of startup zones, UMA and VM, ... */
2003 if (zsize > UMA_SLAB_SPACE) {
2004 /* See keg_large_init(). */
2007 ppera = howmany(roundup2(zsize, UMA_BOOT_ALIGN), PAGE_SIZE);
2008 if (PAGE_SIZE * ppera - roundup2(zsize, UMA_BOOT_ALIGN) <
2011 pages += (zones + vm_zones) * ppera;
2012 } else if (roundup2(zsize, UMA_BOOT_ALIGN) > UMA_SLAB_SPACE)
2013 /* See keg_small_init() special case for uk_ppera = 1. */
2016 pages += howmany(zones,
2017 UMA_SLAB_SPACE / roundup2(zsize, UMA_BOOT_ALIGN));
2019 /* ... and their kegs. Note that zone of zones allocates a keg! */
2020 pages += howmany(zones + 1,
2021 UMA_SLAB_SPACE / roundup2(ksize, UMA_BOOT_ALIGN));
2024 * Most of startup zones are not going to be offpages, that's
2025 * why we use UMA_SLAB_SPACE instead of UMA_SLAB_SIZE in all
2026 * calculations. Some large bucket zones will be offpage, and
2027 * thus will allocate hashes. We take conservative approach
2028 * and assume that all zones may allocate hash. This may give
2029 * us some positive inaccuracy, usually an extra single page.
2031 pages += howmany(zones, UMA_SLAB_SPACE /
2032 (sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT));
2038 uma_startup(void *mem, int npages)
2040 struct uma_zctor_args args;
2041 uma_keg_t masterkeg;
2045 printf("Entering %s with %d boot pages configured\n", __func__, npages);
2048 rw_init(&uma_rwlock, "UMA lock");
2050 /* Use bootpages memory for the zone of zones and zone of kegs. */
2052 zones = (uma_zone_t)m;
2053 m += roundup(zsize, CACHE_LINE_SIZE);
2054 kegs = (uma_zone_t)m;
2055 m += roundup(zsize, CACHE_LINE_SIZE);
2056 masterkeg = (uma_keg_t)m;
2057 m += roundup(ksize, CACHE_LINE_SIZE);
2058 m = roundup(m, PAGE_SIZE);
2059 npages -= (m - (uintptr_t)mem) / PAGE_SIZE;
2062 /* "manually" create the initial zone */
2063 memset(&args, 0, sizeof(args));
2064 args.name = "UMA Kegs";
2066 args.ctor = keg_ctor;
2067 args.dtor = keg_dtor;
2068 args.uminit = zero_init;
2070 args.keg = masterkeg;
2071 args.align = UMA_BOOT_ALIGN - 1;
2072 args.flags = UMA_ZFLAG_INTERNAL;
2073 zone_ctor(kegs, zsize, &args, M_WAITOK);
2076 boot_pages = npages;
2078 args.name = "UMA Zones";
2080 args.ctor = zone_ctor;
2081 args.dtor = zone_dtor;
2082 args.uminit = zero_init;
2085 args.align = UMA_BOOT_ALIGN - 1;
2086 args.flags = UMA_ZFLAG_INTERNAL;
2087 zone_ctor(zones, zsize, &args, M_WAITOK);
2089 /* Now make a zone for slab headers */
2090 slabzone = uma_zcreate("UMA Slabs",
2091 sizeof(struct uma_slab),
2092 NULL, NULL, NULL, NULL,
2093 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2095 hashzone = uma_zcreate("UMA Hash",
2096 sizeof(struct slabhead *) * UMA_HASH_SIZE_INIT,
2097 NULL, NULL, NULL, NULL,
2098 UMA_ALIGN_PTR, UMA_ZFLAG_INTERNAL);
2102 booted = BOOT_STRAPPED;
2110 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2112 booted = BOOT_PAGEALLOC;
2120 printf("Entering %s with %d boot pages left\n", __func__, boot_pages);
2122 booted = BOOT_BUCKETS;
2123 sx_init(&uma_drain_lock, "umadrain");
2128 * Initialize our callout handle
2136 TUNABLE_INT_FETCH("vm.debug.divisor", &dbg_divisor);
2137 uma_dbg_cnt = counter_u64_alloc(M_WAITOK);
2138 uma_skip_cnt = counter_u64_alloc(M_WAITOK);
2140 zone_foreach(zone_alloc_counters);
2141 callout_init(&uma_callout, 1);
2142 callout_reset(&uma_callout, UMA_TIMEOUT * hz, uma_timeout, NULL);
2143 booted = BOOT_RUNNING;
2147 uma_kcreate(uma_zone_t zone, size_t size, uma_init uminit, uma_fini fini,
2148 int align, uint32_t flags)
2150 struct uma_kctor_args args;
2153 args.uminit = uminit;
2155 args.align = (align == UMA_ALIGN_CACHE) ? uma_align_cache : align;
2158 return (zone_alloc_item(kegs, &args, UMA_ANYDOMAIN, M_WAITOK));
2161 /* Public functions */
2164 uma_set_align(int align)
2167 if (align != UMA_ALIGN_CACHE)
2168 uma_align_cache = align;
2173 uma_zcreate(const char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
2174 uma_init uminit, uma_fini fini, int align, uint32_t flags)
2177 struct uma_zctor_args args;
2181 KASSERT(powerof2(align + 1), ("invalid zone alignment %d for \"%s\"",
2184 /* This stuff is essential for the zone ctor */
2185 memset(&args, 0, sizeof(args));
2190 args.uminit = uminit;
2194 * If a zone is being created with an empty constructor and
2195 * destructor, pass UMA constructor/destructor which checks for
2196 * memory use after free.
2198 if ((!(flags & (UMA_ZONE_ZINIT | UMA_ZONE_NOFREE))) &&
2199 ctor == NULL && dtor == NULL && uminit == NULL && fini == NULL) {
2200 args.ctor = trash_ctor;
2201 args.dtor = trash_dtor;
2202 args.uminit = trash_init;
2203 args.fini = trash_fini;
2210 if (booted < BOOT_BUCKETS) {
2213 sx_slock(&uma_drain_lock);
2216 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2218 sx_sunlock(&uma_drain_lock);
2224 uma_zsecond_create(char *name, uma_ctor ctor, uma_dtor dtor,
2225 uma_init zinit, uma_fini zfini, uma_zone_t master)
2227 struct uma_zctor_args args;
2232 keg = master->uz_keg;
2233 memset(&args, 0, sizeof(args));
2235 args.size = keg->uk_size;
2238 args.uminit = zinit;
2240 args.align = keg->uk_align;
2241 args.flags = keg->uk_flags | UMA_ZONE_SECONDARY;
2244 if (booted < BOOT_BUCKETS) {
2247 sx_slock(&uma_drain_lock);
2250 /* XXX Attaches only one keg of potentially many. */
2251 res = zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK);
2253 sx_sunlock(&uma_drain_lock);
2259 uma_zcache_create(char *name, int size, uma_ctor ctor, uma_dtor dtor,
2260 uma_init zinit, uma_fini zfini, uma_import zimport,
2261 uma_release zrelease, void *arg, int flags)
2263 struct uma_zctor_args args;
2265 memset(&args, 0, sizeof(args));
2270 args.uminit = zinit;
2272 args.import = zimport;
2273 args.release = zrelease;
2276 args.flags = flags | UMA_ZFLAG_CACHE;
2278 return (zone_alloc_item(zones, &args, UMA_ANYDOMAIN, M_WAITOK));
2283 uma_zdestroy(uma_zone_t zone)
2286 sx_slock(&uma_drain_lock);
2287 zone_free_item(zones, zone, NULL, SKIP_NONE);
2288 sx_sunlock(&uma_drain_lock);
2292 uma_zwait(uma_zone_t zone)
2296 item = uma_zalloc_arg(zone, NULL, M_WAITOK);
2297 uma_zfree(zone, item);
2301 uma_zalloc_pcpu_arg(uma_zone_t zone, void *udata, int flags)
2307 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2309 item = uma_zalloc_arg(zone, udata, flags & ~M_ZERO);
2310 if (item != NULL && (flags & M_ZERO)) {
2312 for (i = 0; i <= mp_maxid; i++)
2313 bzero(zpcpu_get_cpu(item, i), zone->uz_size);
2315 bzero(item, zone->uz_size);
2322 * A stub while both regular and pcpu cases are identical.
2325 uma_zfree_pcpu_arg(uma_zone_t zone, void *item, void *udata)
2329 MPASS(zone->uz_flags & UMA_ZONE_PCPU);
2331 uma_zfree_arg(zone, item, udata);
2336 uma_zalloc_arg(uma_zone_t zone, void *udata, int flags)
2338 uma_zone_domain_t zdom;
2339 uma_bucket_t bucket;
2342 int cpu, domain, lockfail, maxbucket;
2347 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2348 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2350 /* This is the fast path allocation */
2351 CTR4(KTR_UMA, "uma_zalloc_arg thread %x zone %s(%p) flags %d",
2352 curthread, zone->uz_name, zone, flags);
2354 if (flags & M_WAITOK) {
2355 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2356 "uma_zalloc_arg: zone \"%s\"", zone->uz_name);
2358 KASSERT((flags & M_EXEC) == 0, ("uma_zalloc_arg: called with M_EXEC"));
2359 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2360 ("uma_zalloc_arg: called with spinlock or critical section held"));
2361 if (zone->uz_flags & UMA_ZONE_PCPU)
2362 KASSERT((flags & M_ZERO) == 0, ("allocating from a pcpu zone "
2363 "with M_ZERO passed"));
2365 #ifdef DEBUG_MEMGUARD
2366 if (memguard_cmp_zone(zone)) {
2367 item = memguard_alloc(zone->uz_size, flags);
2369 if (zone->uz_init != NULL &&
2370 zone->uz_init(item, zone->uz_size, flags) != 0)
2372 if (zone->uz_ctor != NULL &&
2373 zone->uz_ctor(item, zone->uz_size, udata,
2375 zone->uz_fini(item, zone->uz_size);
2380 /* This is unfortunate but should not be fatal. */
2384 * If possible, allocate from the per-CPU cache. There are two
2385 * requirements for safe access to the per-CPU cache: (1) the thread
2386 * accessing the cache must not be preempted or yield during access,
2387 * and (2) the thread must not migrate CPUs without switching which
2388 * cache it accesses. We rely on a critical section to prevent
2389 * preemption and migration. We release the critical section in
2390 * order to acquire the zone mutex if we are unable to allocate from
2391 * the current cache; when we re-acquire the critical section, we
2392 * must detect and handle migration if it has occurred.
2397 cache = &zone->uz_cpu[cpu];
2400 bucket = cache->uc_allocbucket;
2401 if (bucket != NULL && bucket->ub_cnt > 0) {
2403 item = bucket->ub_bucket[bucket->ub_cnt];
2405 bucket->ub_bucket[bucket->ub_cnt] = NULL;
2407 KASSERT(item != NULL, ("uma_zalloc: Bucket pointer mangled."));
2411 skipdbg = uma_dbg_zskip(zone, item);
2413 if (zone->uz_ctor != NULL &&
2415 (!skipdbg || zone->uz_ctor != trash_ctor ||
2416 zone->uz_dtor != trash_dtor) &&
2418 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2419 counter_u64_add(zone->uz_fails, 1);
2420 zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
2425 uma_dbg_alloc(zone, NULL, item);
2428 uma_zero_item(item, zone);
2433 * We have run out of items in our alloc bucket.
2434 * See if we can switch with our free bucket.
2436 bucket = cache->uc_freebucket;
2437 if (bucket != NULL && bucket->ub_cnt > 0) {
2439 "uma_zalloc: zone %s(%p) swapping empty with alloc",
2440 zone->uz_name, zone);
2441 cache->uc_freebucket = cache->uc_allocbucket;
2442 cache->uc_allocbucket = bucket;
2447 * Discard any empty allocation bucket while we hold no locks.
2449 bucket = cache->uc_allocbucket;
2450 cache->uc_allocbucket = NULL;
2453 bucket_free(zone, bucket, udata);
2455 if (zone->uz_flags & UMA_ZONE_NUMA) {
2456 domain = PCPU_GET(domain);
2457 if (VM_DOMAIN_EMPTY(domain))
2458 domain = UMA_ANYDOMAIN;
2460 domain = UMA_ANYDOMAIN;
2462 /* Short-circuit for zones without buckets and low memory. */
2463 if (zone->uz_count == 0 || bucketdisable) {
2469 * Attempt to retrieve the item from the per-CPU cache has failed, so
2470 * we must go back to the zone. This requires the zone lock, so we
2471 * must drop the critical section, then re-acquire it when we go back
2472 * to the cache. Since the critical section is released, we may be
2473 * preempted or migrate. As such, make sure not to maintain any
2474 * thread-local state specific to the cache from prior to releasing
2475 * the critical section.
2478 if (ZONE_TRYLOCK(zone) == 0) {
2479 /* Record contention to size the buckets. */
2485 cache = &zone->uz_cpu[cpu];
2487 /* See if we lost the race to fill the cache. */
2488 if (cache->uc_allocbucket != NULL) {
2494 * Check the zone's cache of buckets.
2496 if (domain == UMA_ANYDOMAIN)
2497 zdom = &zone->uz_domain[0];
2499 zdom = &zone->uz_domain[domain];
2500 if ((bucket = zone_try_fetch_bucket(zone, zdom, true)) != NULL) {
2501 KASSERT(bucket->ub_cnt != 0,
2502 ("uma_zalloc_arg: Returning an empty bucket."));
2503 cache->uc_allocbucket = bucket;
2507 /* We are no longer associated with this CPU. */
2511 * We bump the uz count when the cache size is insufficient to
2512 * handle the working set.
2514 if (lockfail && zone->uz_count < zone->uz_count_max)
2517 if (zone->uz_max_items > 0) {
2518 if (zone->uz_items >= zone->uz_max_items)
2520 maxbucket = MIN(zone->uz_count,
2521 zone->uz_max_items - zone->uz_items);
2522 zone->uz_items += maxbucket;
2524 maxbucket = zone->uz_count;
2528 * Now lets just fill a bucket and put it on the free list. If that
2529 * works we'll restart the allocation from the beginning and it
2530 * will use the just filled bucket.
2532 bucket = zone_alloc_bucket(zone, udata, domain, flags, maxbucket);
2533 CTR3(KTR_UMA, "uma_zalloc: zone %s(%p) bucket zone returned %p",
2534 zone->uz_name, zone, bucket);
2536 if (bucket != NULL) {
2537 if (zone->uz_max_items > 0 && bucket->ub_cnt < maxbucket) {
2538 MPASS(zone->uz_items >= maxbucket - bucket->ub_cnt);
2539 zone->uz_items -= maxbucket - bucket->ub_cnt;
2540 if (zone->uz_sleepers > 0 &&
2541 zone->uz_items < zone->uz_max_items)
2546 cache = &zone->uz_cpu[cpu];
2549 * See if we lost the race or were migrated. Cache the
2550 * initialized bucket to make this less likely or claim
2551 * the memory directly.
2553 if (cache->uc_allocbucket == NULL &&
2554 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
2555 domain == PCPU_GET(domain))) {
2556 cache->uc_allocbucket = bucket;
2557 zdom->uzd_imax += bucket->ub_cnt;
2558 } else if (zone->uz_bkt_count >= zone->uz_bkt_max) {
2561 bucket_drain(zone, bucket);
2562 bucket_free(zone, bucket, udata);
2563 goto zalloc_restart;
2565 zone_put_bucket(zone, zdom, bucket, false);
2568 } else if (zone->uz_max_items > 0) {
2569 zone->uz_items -= maxbucket;
2570 if (zone->uz_sleepers > 0 &&
2571 zone->uz_items + 1 < zone->uz_max_items)
2576 * We may not be able to get a bucket so return an actual item.
2579 item = zone_alloc_item_locked(zone, udata, domain, flags);
2585 uma_zalloc_domain(uma_zone_t zone, void *udata, int domain, int flags)
2588 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
2589 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
2591 /* This is the fast path allocation */
2593 "uma_zalloc_domain thread %x zone %s(%p) domain %d flags %d",
2594 curthread, zone->uz_name, zone, domain, flags);
2596 if (flags & M_WAITOK) {
2597 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
2598 "uma_zalloc_domain: zone \"%s\"", zone->uz_name);
2600 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
2601 ("uma_zalloc_domain: called with spinlock or critical section held"));
2603 return (zone_alloc_item(zone, udata, domain, flags));
2607 * Find a slab with some space. Prefer slabs that are partially used over those
2608 * that are totally full. This helps to reduce fragmentation.
2610 * If 'rr' is 1, search all domains starting from 'domain'. Otherwise check
2614 keg_first_slab(uma_keg_t keg, int domain, bool rr)
2620 KASSERT(domain >= 0 && domain < vm_ndomains,
2621 ("keg_first_slab: domain %d out of range", domain));
2622 KEG_LOCK_ASSERT(keg);
2627 dom = &keg->uk_domain[domain];
2628 if (!LIST_EMPTY(&dom->ud_part_slab))
2629 return (LIST_FIRST(&dom->ud_part_slab));
2630 if (!LIST_EMPTY(&dom->ud_free_slab)) {
2631 slab = LIST_FIRST(&dom->ud_free_slab);
2632 LIST_REMOVE(slab, us_link);
2633 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2637 domain = (domain + 1) % vm_ndomains;
2638 } while (domain != start);
2644 keg_fetch_free_slab(uma_keg_t keg, int domain, bool rr, int flags)
2648 KEG_LOCK_ASSERT(keg);
2650 reserve = (flags & M_USE_RESERVE) != 0 ? 0 : keg->uk_reserve;
2651 if (keg->uk_free <= reserve)
2653 return (keg_first_slab(keg, domain, rr));
2657 keg_fetch_slab(uma_keg_t keg, uma_zone_t zone, int rdomain, const int flags)
2659 struct vm_domainset_iter di;
2666 KEG_LOCK_ASSERT(keg);
2669 * Use the keg's policy if upper layers haven't already specified a
2670 * domain (as happens with first-touch zones).
2672 * To avoid races we run the iterator with the keg lock held, but that
2673 * means that we cannot allow the vm_domainset layer to sleep. Thus,
2674 * clear M_WAITOK and handle low memory conditions locally.
2676 rr = rdomain == UMA_ANYDOMAIN;
2678 aflags = (flags & ~M_WAITOK) | M_NOWAIT;
2679 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
2687 slab = keg_fetch_free_slab(keg, domain, rr, flags);
2689 MPASS(slab->us_keg == keg);
2694 * M_NOVM means don't ask at all!
2699 KASSERT(zone->uz_max_items == 0 ||
2700 zone->uz_items <= zone->uz_max_items,
2701 ("%s: zone %p overflow", __func__, zone));
2703 slab = keg_alloc_slab(keg, zone, domain, flags, aflags);
2705 * If we got a slab here it's safe to mark it partially used
2706 * and return. We assume that the caller is going to remove
2707 * at least one item.
2710 MPASS(slab->us_keg == keg);
2711 dom = &keg->uk_domain[slab->us_domain];
2712 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
2716 if (rr && vm_domainset_iter_policy(&di, &domain) != 0) {
2717 if ((flags & M_WAITOK) != 0) {
2719 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
2728 * We might not have been able to get a slab but another cpu
2729 * could have while we were unlocked. Check again before we
2732 if ((slab = keg_fetch_free_slab(keg, domain, rr, flags)) != NULL) {
2733 MPASS(slab->us_keg == keg);
2740 zone_fetch_slab(uma_zone_t zone, uma_keg_t keg, int domain, int flags)
2750 slab = keg_fetch_slab(keg, zone, domain, flags);
2753 if (flags & (M_NOWAIT | M_NOVM))
2761 slab_alloc_item(uma_keg_t keg, uma_slab_t slab)
2767 MPASS(keg == slab->us_keg);
2768 KEG_LOCK_ASSERT(keg);
2770 freei = BIT_FFS(SLAB_SETSIZE, &slab->us_free) - 1;
2771 BIT_CLR(SLAB_SETSIZE, freei, &slab->us_free);
2772 item = slab->us_data + (keg->uk_rsize * freei);
2773 slab->us_freecount--;
2776 /* Move this slab to the full list */
2777 if (slab->us_freecount == 0) {
2778 LIST_REMOVE(slab, us_link);
2779 dom = &keg->uk_domain[slab->us_domain];
2780 LIST_INSERT_HEAD(&dom->ud_full_slab, slab, us_link);
2787 zone_import(uma_zone_t zone, void **bucket, int max, int domain, int flags)
2798 /* Try to keep the buckets totally full */
2799 for (i = 0; i < max; ) {
2800 if ((slab = zone->uz_slab(zone, keg, domain, flags)) == NULL)
2804 stripe = howmany(max, vm_ndomains);
2806 while (slab->us_freecount && i < max) {
2807 bucket[i++] = slab_alloc_item(keg, slab);
2808 if (keg->uk_free <= keg->uk_reserve)
2812 * If the zone is striped we pick a new slab for every
2813 * N allocations. Eliminating this conditional will
2814 * instead pick a new domain for each bucket rather
2815 * than stripe within each bucket. The current option
2816 * produces more fragmentation and requires more cpu
2817 * time but yields better distribution.
2819 if ((zone->uz_flags & UMA_ZONE_NUMA) == 0 &&
2820 vm_ndomains > 1 && --stripe == 0)
2824 /* Don't block if we allocated any successfully. */
2835 zone_alloc_bucket(uma_zone_t zone, void *udata, int domain, int flags, int max)
2837 uma_bucket_t bucket;
2839 CTR1(KTR_UMA, "zone_alloc:_bucket domain %d)", domain);
2841 /* Don't wait for buckets, preserve caller's NOVM setting. */
2842 bucket = bucket_alloc(zone, udata, M_NOWAIT | (flags & M_NOVM));
2846 bucket->ub_cnt = zone->uz_import(zone->uz_arg, bucket->ub_bucket,
2847 MIN(max, bucket->ub_entries), domain, flags);
2850 * Initialize the memory if necessary.
2852 if (bucket->ub_cnt != 0 && zone->uz_init != NULL) {
2855 for (i = 0; i < bucket->ub_cnt; i++)
2856 if (zone->uz_init(bucket->ub_bucket[i], zone->uz_size,
2860 * If we couldn't initialize the whole bucket, put the
2861 * rest back onto the freelist.
2863 if (i != bucket->ub_cnt) {
2864 zone->uz_release(zone->uz_arg, &bucket->ub_bucket[i],
2865 bucket->ub_cnt - i);
2867 bzero(&bucket->ub_bucket[i],
2868 sizeof(void *) * (bucket->ub_cnt - i));
2874 if (bucket->ub_cnt == 0) {
2875 bucket_free(zone, bucket, udata);
2876 counter_u64_add(zone->uz_fails, 1);
2884 * Allocates a single item from a zone.
2887 * zone The zone to alloc for.
2888 * udata The data to be passed to the constructor.
2889 * domain The domain to allocate from or UMA_ANYDOMAIN.
2890 * flags M_WAITOK, M_NOWAIT, M_ZERO.
2893 * NULL if there is no memory and M_NOWAIT is set
2894 * An item if successful
2898 zone_alloc_item(uma_zone_t zone, void *udata, int domain, int flags)
2902 return (zone_alloc_item_locked(zone, udata, domain, flags));
2906 * Returns with zone unlocked.
2909 zone_alloc_item_locked(uma_zone_t zone, void *udata, int domain, int flags)
2916 ZONE_LOCK_ASSERT(zone);
2918 if (zone->uz_max_items > 0) {
2919 if (zone->uz_items >= zone->uz_max_items) {
2920 zone_log_warning(zone);
2921 zone_maxaction(zone);
2922 if (flags & M_NOWAIT) {
2927 zone->uz_sleepers++;
2928 while (zone->uz_items >= zone->uz_max_items)
2929 mtx_sleep(zone, zone->uz_lockptr, PVM,
2931 zone->uz_sleepers--;
2932 if (zone->uz_sleepers > 0 &&
2933 zone->uz_items + 1 < zone->uz_max_items)
2940 if (domain != UMA_ANYDOMAIN) {
2941 /* avoid allocs targeting empty domains */
2942 if (VM_DOMAIN_EMPTY(domain))
2943 domain = UMA_ANYDOMAIN;
2945 if (zone->uz_import(zone->uz_arg, &item, 1, domain, flags) != 1)
2949 skipdbg = uma_dbg_zskip(zone, item);
2952 * We have to call both the zone's init (not the keg's init)
2953 * and the zone's ctor. This is because the item is going from
2954 * a keg slab directly to the user, and the user is expecting it
2955 * to be both zone-init'd as well as zone-ctor'd.
2957 if (zone->uz_init != NULL) {
2958 if (zone->uz_init(item, zone->uz_size, flags) != 0) {
2959 zone_free_item(zone, item, udata, SKIP_FINI | SKIP_CNT);
2963 if (zone->uz_ctor != NULL &&
2965 (!skipdbg || zone->uz_ctor != trash_ctor ||
2966 zone->uz_dtor != trash_dtor) &&
2968 zone->uz_ctor(item, zone->uz_size, udata, flags) != 0) {
2969 zone_free_item(zone, item, udata, SKIP_DTOR | SKIP_CNT);
2974 uma_dbg_alloc(zone, NULL, item);
2977 uma_zero_item(item, zone);
2979 counter_u64_add(zone->uz_allocs, 1);
2980 CTR3(KTR_UMA, "zone_alloc_item item %p from %s(%p)", item,
2981 zone->uz_name, zone);
2986 if (zone->uz_max_items > 0) {
2991 counter_u64_add(zone->uz_fails, 1);
2992 CTR2(KTR_UMA, "zone_alloc_item failed from %s(%p)",
2993 zone->uz_name, zone);
2999 uma_zfree_arg(uma_zone_t zone, void *item, void *udata)
3002 uma_bucket_t bucket;
3003 uma_zone_domain_t zdom;
3010 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3011 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3013 CTR2(KTR_UMA, "uma_zfree_arg thread %x zone %s", curthread,
3016 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3017 ("uma_zfree_arg: called with spinlock or critical section held"));
3019 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3022 #ifdef DEBUG_MEMGUARD
3023 if (is_memguard_addr(item)) {
3024 if (zone->uz_dtor != NULL)
3025 zone->uz_dtor(item, zone->uz_size, udata);
3026 if (zone->uz_fini != NULL)
3027 zone->uz_fini(item, zone->uz_size);
3028 memguard_free(item);
3033 skipdbg = uma_dbg_zskip(zone, item);
3034 if (skipdbg == false) {
3035 if (zone->uz_flags & UMA_ZONE_MALLOC)
3036 uma_dbg_free(zone, udata, item);
3038 uma_dbg_free(zone, NULL, item);
3040 if (zone->uz_dtor != NULL && (!skipdbg ||
3041 zone->uz_dtor != trash_dtor || zone->uz_ctor != trash_ctor))
3043 if (zone->uz_dtor != NULL)
3045 zone->uz_dtor(item, zone->uz_size, udata);
3048 * The race here is acceptable. If we miss it we'll just have to wait
3049 * a little longer for the limits to be reset.
3051 if (zone->uz_sleepers > 0)
3055 * If possible, free to the per-CPU cache. There are two
3056 * requirements for safe access to the per-CPU cache: (1) the thread
3057 * accessing the cache must not be preempted or yield during access,
3058 * and (2) the thread must not migrate CPUs without switching which
3059 * cache it accesses. We rely on a critical section to prevent
3060 * preemption and migration. We release the critical section in
3061 * order to acquire the zone mutex if we are unable to free to the
3062 * current cache; when we re-acquire the critical section, we must
3063 * detect and handle migration if it has occurred.
3068 cache = &zone->uz_cpu[cpu];
3072 * Try to free into the allocbucket first to give LIFO ordering
3073 * for cache-hot datastructures. Spill over into the freebucket
3074 * if necessary. Alloc will swap them if one runs dry.
3076 bucket = cache->uc_allocbucket;
3077 if (bucket == NULL || bucket->ub_cnt >= bucket->ub_entries)
3078 bucket = cache->uc_freebucket;
3079 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3080 KASSERT(bucket->ub_bucket[bucket->ub_cnt] == NULL,
3081 ("uma_zfree: Freeing to non free bucket index."));
3082 bucket->ub_bucket[bucket->ub_cnt] = item;
3090 * We must go back the zone, which requires acquiring the zone lock,
3091 * which in turn means we must release and re-acquire the critical
3092 * section. Since the critical section is released, we may be
3093 * preempted or migrate. As such, make sure not to maintain any
3094 * thread-local state specific to the cache from prior to releasing
3095 * the critical section.
3098 if (zone->uz_count == 0 || bucketdisable)
3102 if (ZONE_TRYLOCK(zone) == 0) {
3103 /* Record contention to size the buckets. */
3109 cache = &zone->uz_cpu[cpu];
3111 bucket = cache->uc_freebucket;
3112 if (bucket != NULL && bucket->ub_cnt < bucket->ub_entries) {
3116 cache->uc_freebucket = NULL;
3117 /* We are no longer associated with this CPU. */
3120 if ((zone->uz_flags & UMA_ZONE_NUMA) != 0) {
3121 domain = PCPU_GET(domain);
3122 if (VM_DOMAIN_EMPTY(domain))
3123 domain = UMA_ANYDOMAIN;
3126 zdom = &zone->uz_domain[0];
3128 /* Can we throw this on the zone full list? */
3129 if (bucket != NULL) {
3131 "uma_zfree: zone %s(%p) putting bucket %p on free list",
3132 zone->uz_name, zone, bucket);
3133 /* ub_cnt is pointing to the last free item */
3134 KASSERT(bucket->ub_cnt == bucket->ub_entries,
3135 ("uma_zfree: Attempting to insert not full bucket onto the full list.\n"));
3136 if (zone->uz_bkt_count >= zone->uz_bkt_max) {
3138 bucket_drain(zone, bucket);
3139 bucket_free(zone, bucket, udata);
3142 zone_put_bucket(zone, zdom, bucket, true);
3146 * We bump the uz count when the cache size is insufficient to
3147 * handle the working set.
3149 if (lockfail && zone->uz_count < zone->uz_count_max)
3153 bucket = bucket_alloc(zone, udata, M_NOWAIT);
3154 CTR3(KTR_UMA, "uma_zfree: zone %s(%p) allocated bucket %p",
3155 zone->uz_name, zone, bucket);
3159 cache = &zone->uz_cpu[cpu];
3160 if (cache->uc_freebucket == NULL &&
3161 ((zone->uz_flags & UMA_ZONE_NUMA) == 0 ||
3162 domain == PCPU_GET(domain))) {
3163 cache->uc_freebucket = bucket;
3167 * We lost the race, start over. We have to drop our
3168 * critical section to free the bucket.
3171 bucket_free(zone, bucket, udata);
3176 * If nothing else caught this, we'll just do an internal free.
3179 zone_free_item(zone, item, udata, SKIP_DTOR);
3183 uma_zfree_domain(uma_zone_t zone, void *item, void *udata)
3186 /* Enable entropy collection for RANDOM_ENABLE_UMA kernel option */
3187 random_harvest_fast_uma(&zone, sizeof(zone), RANDOM_UMA);
3189 CTR2(KTR_UMA, "uma_zfree_domain thread %x zone %s", curthread,
3192 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
3193 ("uma_zfree_domain: called with spinlock or critical section held"));
3195 /* uma_zfree(..., NULL) does nothing, to match free(9). */
3198 zone_free_item(zone, item, udata, SKIP_NONE);
3202 slab_free_item(uma_zone_t zone, uma_slab_t slab, void *item)
3209 MPASS(zone->uz_lockptr == &keg->uk_lock);
3210 KEG_LOCK_ASSERT(keg);
3211 MPASS(keg == slab->us_keg);
3213 dom = &keg->uk_domain[slab->us_domain];
3215 /* Do we need to remove from any lists? */
3216 if (slab->us_freecount+1 == keg->uk_ipers) {
3217 LIST_REMOVE(slab, us_link);
3218 LIST_INSERT_HEAD(&dom->ud_free_slab, slab, us_link);
3219 } else if (slab->us_freecount == 0) {
3220 LIST_REMOVE(slab, us_link);
3221 LIST_INSERT_HEAD(&dom->ud_part_slab, slab, us_link);
3224 /* Slab management. */
3225 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
3226 BIT_SET(SLAB_SETSIZE, freei, &slab->us_free);
3227 slab->us_freecount++;
3229 /* Keg statistics. */
3234 zone_release(uma_zone_t zone, void **bucket, int cnt)
3244 for (i = 0; i < cnt; i++) {
3246 if (!(zone->uz_flags & UMA_ZONE_VTOSLAB)) {
3247 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
3248 if (zone->uz_flags & UMA_ZONE_HASH) {
3249 slab = hash_sfind(&keg->uk_hash, mem);
3251 mem += keg->uk_pgoff;
3252 slab = (uma_slab_t)mem;
3255 slab = vtoslab((vm_offset_t)item);
3256 MPASS(slab->us_keg == keg);
3258 slab_free_item(zone, slab, item);
3264 * Frees a single item to any zone.
3267 * zone The zone to free to
3268 * item The item we're freeing
3269 * udata User supplied data for the dtor
3270 * skip Skip dtors and finis
3273 zone_free_item(uma_zone_t zone, void *item, void *udata, enum zfreeskip skip)
3278 skipdbg = uma_dbg_zskip(zone, item);
3279 if (skip == SKIP_NONE && !skipdbg) {
3280 if (zone->uz_flags & UMA_ZONE_MALLOC)
3281 uma_dbg_free(zone, udata, item);
3283 uma_dbg_free(zone, NULL, item);
3286 if (skip < SKIP_DTOR && zone->uz_dtor != NULL &&
3287 (!skipdbg || zone->uz_dtor != trash_dtor ||
3288 zone->uz_ctor != trash_ctor))
3290 if (skip < SKIP_DTOR && zone->uz_dtor != NULL)
3292 zone->uz_dtor(item, zone->uz_size, udata);
3294 if (skip < SKIP_FINI && zone->uz_fini)
3295 zone->uz_fini(item, zone->uz_size);
3297 zone->uz_release(zone->uz_arg, &item, 1);
3299 if (skip & SKIP_CNT)
3302 counter_u64_add(zone->uz_frees, 1);
3304 if (zone->uz_max_items > 0) {
3307 if (zone->uz_sleepers > 0 &&
3308 zone->uz_items < zone->uz_max_items)
3316 uma_zone_set_max(uma_zone_t zone, int nitems)
3318 struct uma_bucket_zone *ubz;
3321 * If limit is very low we may need to limit how
3322 * much items are allowed in CPU caches.
3324 ubz = &bucket_zones[0];
3325 for (; ubz->ubz_entries != 0; ubz++)
3326 if (ubz->ubz_entries * 2 * mp_ncpus > nitems)
3328 if (ubz == &bucket_zones[0])
3329 nitems = ubz->ubz_entries * 2 * mp_ncpus;
3334 zone->uz_count_max = zone->uz_count = ubz->ubz_entries;
3335 if (zone->uz_count_min > zone->uz_count_max)
3336 zone->uz_count_min = zone->uz_count_max;
3337 zone->uz_max_items = nitems;
3345 uma_zone_set_maxcache(uma_zone_t zone, int nitems)
3349 zone->uz_bkt_max = nitems;
3357 uma_zone_get_max(uma_zone_t zone)
3362 nitems = zone->uz_max_items;
3370 uma_zone_set_warning(uma_zone_t zone, const char *warning)
3374 zone->uz_warning = warning;
3380 uma_zone_set_maxaction(uma_zone_t zone, uma_maxaction_t maxaction)
3384 TASK_INIT(&zone->uz_maxaction, 0, (task_fn_t *)maxaction, zone);
3390 uma_zone_get_cur(uma_zone_t zone)
3396 nitems = counter_u64_fetch(zone->uz_allocs) -
3397 counter_u64_fetch(zone->uz_frees);
3400 * See the comment in sysctl_vm_zone_stats() regarding the
3401 * safety of accessing the per-cpu caches. With the zone lock
3402 * held, it is safe, but can potentially result in stale data.
3404 nitems += zone->uz_cpu[i].uc_allocs -
3405 zone->uz_cpu[i].uc_frees;
3409 return (nitems < 0 ? 0 : nitems);
3414 uma_zone_set_init(uma_zone_t zone, uma_init uminit)
3420 KASSERT(keg->uk_pages == 0,
3421 ("uma_zone_set_init on non-empty keg"));
3422 keg->uk_init = uminit;
3428 uma_zone_set_fini(uma_zone_t zone, uma_fini fini)
3434 KASSERT(keg->uk_pages == 0,
3435 ("uma_zone_set_fini on non-empty keg"));
3436 keg->uk_fini = fini;
3442 uma_zone_set_zinit(uma_zone_t zone, uma_init zinit)
3446 KASSERT(zone->uz_keg->uk_pages == 0,
3447 ("uma_zone_set_zinit on non-empty keg"));
3448 zone->uz_init = zinit;
3454 uma_zone_set_zfini(uma_zone_t zone, uma_fini zfini)
3458 KASSERT(zone->uz_keg->uk_pages == 0,
3459 ("uma_zone_set_zfini on non-empty keg"));
3460 zone->uz_fini = zfini;
3465 /* XXX uk_freef is not actually used with the zone locked */
3467 uma_zone_set_freef(uma_zone_t zone, uma_free freef)
3472 KASSERT(keg != NULL, ("uma_zone_set_freef: Invalid zone type"));
3474 keg->uk_freef = freef;
3479 /* XXX uk_allocf is not actually used with the zone locked */
3481 uma_zone_set_allocf(uma_zone_t zone, uma_alloc allocf)
3487 keg->uk_allocf = allocf;
3493 uma_zone_reserve(uma_zone_t zone, int items)
3499 keg->uk_reserve = items;
3505 uma_zone_reserve_kva(uma_zone_t zone, int count)
3513 pages = count / keg->uk_ipers;
3514 if (pages * keg->uk_ipers < count)
3516 pages *= keg->uk_ppera;
3518 #ifdef UMA_MD_SMALL_ALLOC
3519 if (keg->uk_ppera > 1) {
3523 kva = kva_alloc((vm_size_t)pages * PAGE_SIZE);
3530 MPASS(keg->uk_kva == 0);
3533 zone->uz_max_items = pages * keg->uk_ipers;
3534 #ifdef UMA_MD_SMALL_ALLOC
3535 keg->uk_allocf = (keg->uk_ppera > 1) ? noobj_alloc : uma_small_alloc;
3537 keg->uk_allocf = noobj_alloc;
3539 keg->uk_flags |= UMA_ZONE_NOFREE;
3547 uma_prealloc(uma_zone_t zone, int items)
3549 struct vm_domainset_iter di;
3553 int aflags, domain, slabs;
3557 slabs = items / keg->uk_ipers;
3558 if (slabs * keg->uk_ipers < items)
3560 while (slabs-- > 0) {
3562 vm_domainset_iter_policy_ref_init(&di, &keg->uk_dr, &domain,
3565 slab = keg_alloc_slab(keg, zone, domain, M_WAITOK,
3568 MPASS(slab->us_keg == keg);
3569 dom = &keg->uk_domain[slab->us_domain];
3570 LIST_INSERT_HEAD(&dom->ud_free_slab, slab,
3575 if (vm_domainset_iter_policy(&di, &domain) != 0) {
3577 vm_wait_doms(&keg->uk_dr.dr_policy->ds_mask);
3587 uma_reclaim_locked(bool kmem_danger)
3590 CTR0(KTR_UMA, "UMA: vm asked us to release pages!");
3591 sx_assert(&uma_drain_lock, SA_XLOCKED);
3593 zone_foreach(zone_drain);
3594 if (vm_page_count_min() || kmem_danger) {
3595 cache_drain_safe(NULL);
3596 zone_foreach(zone_drain);
3600 * Some slabs may have been freed but this zone will be visited early
3601 * we visit again so that we can free pages that are empty once other
3602 * zones are drained. We have to do the same for buckets.
3604 zone_drain(slabzone);
3605 bucket_zone_drain();
3612 sx_xlock(&uma_drain_lock);
3613 uma_reclaim_locked(false);
3614 sx_xunlock(&uma_drain_lock);
3617 static volatile int uma_reclaim_needed;
3620 uma_reclaim_wakeup(void)
3623 if (atomic_fetchadd_int(&uma_reclaim_needed, 1) == 0)
3624 wakeup(uma_reclaim);
3628 uma_reclaim_worker(void *arg __unused)
3632 sx_xlock(&uma_drain_lock);
3633 while (atomic_load_int(&uma_reclaim_needed) == 0)
3634 sx_sleep(uma_reclaim, &uma_drain_lock, PVM, "umarcl",
3636 sx_xunlock(&uma_drain_lock);
3637 EVENTHANDLER_INVOKE(vm_lowmem, VM_LOW_KMEM);
3638 sx_xlock(&uma_drain_lock);
3639 uma_reclaim_locked(true);
3640 atomic_store_int(&uma_reclaim_needed, 0);
3641 sx_xunlock(&uma_drain_lock);
3642 /* Don't fire more than once per-second. */
3643 pause("umarclslp", hz);
3649 uma_zone_exhausted(uma_zone_t zone)
3654 full = zone->uz_sleepers > 0;
3660 uma_zone_exhausted_nolock(uma_zone_t zone)
3662 return (zone->uz_sleepers > 0);
3666 uma_large_malloc_domain(vm_size_t size, int domain, int wait)
3668 struct domainset *policy;
3672 if (domain != UMA_ANYDOMAIN) {
3673 /* avoid allocs targeting empty domains */
3674 if (VM_DOMAIN_EMPTY(domain))
3675 domain = UMA_ANYDOMAIN;
3677 slab = zone_alloc_item(slabzone, NULL, domain, wait);
3680 policy = (domain == UMA_ANYDOMAIN) ? DOMAINSET_RR() :
3681 DOMAINSET_FIXED(domain);
3682 addr = kmem_malloc_domainset(policy, size, wait);
3684 vsetslab(addr, slab);
3685 slab->us_data = (void *)addr;
3686 slab->us_flags = UMA_SLAB_KERNEL | UMA_SLAB_MALLOC;
3687 slab->us_size = size;
3688 slab->us_domain = vm_phys_domain(PHYS_TO_VM_PAGE(
3689 pmap_kextract(addr)));
3690 uma_total_inc(size);
3692 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3695 return ((void *)addr);
3699 uma_large_malloc(vm_size_t size, int wait)
3702 return uma_large_malloc_domain(size, UMA_ANYDOMAIN, wait);
3706 uma_large_free(uma_slab_t slab)
3709 KASSERT((slab->us_flags & UMA_SLAB_KERNEL) != 0,
3710 ("uma_large_free: Memory not allocated with uma_large_malloc."));
3711 kmem_free((vm_offset_t)slab->us_data, slab->us_size);
3712 uma_total_dec(slab->us_size);
3713 zone_free_item(slabzone, slab, NULL, SKIP_NONE);
3717 uma_zero_item(void *item, uma_zone_t zone)
3720 bzero(item, zone->uz_size);
3727 return (uma_kmem_limit);
3731 uma_set_limit(unsigned long limit)
3734 uma_kmem_limit = limit;
3741 return (uma_kmem_total);
3748 return (uma_kmem_limit - uma_kmem_total);
3752 uma_print_stats(void)
3754 zone_foreach(uma_print_zone);
3758 slab_print(uma_slab_t slab)
3760 printf("slab: keg %p, data %p, freecount %d\n",
3761 slab->us_keg, slab->us_data, slab->us_freecount);
3765 cache_print(uma_cache_t cache)
3767 printf("alloc: %p(%d), free: %p(%d)\n",
3768 cache->uc_allocbucket,
3769 cache->uc_allocbucket?cache->uc_allocbucket->ub_cnt:0,
3770 cache->uc_freebucket,
3771 cache->uc_freebucket?cache->uc_freebucket->ub_cnt:0);
3775 uma_print_keg(uma_keg_t keg)
3781 printf("keg: %s(%p) size %d(%d) flags %#x ipers %d ppera %d "
3783 keg->uk_name, keg, keg->uk_size, keg->uk_rsize, keg->uk_flags,
3784 keg->uk_ipers, keg->uk_ppera,
3785 (keg->uk_pages / keg->uk_ppera) * keg->uk_ipers - keg->uk_free,
3787 for (i = 0; i < vm_ndomains; i++) {
3788 dom = &keg->uk_domain[i];
3789 printf("Part slabs:\n");
3790 LIST_FOREACH(slab, &dom->ud_part_slab, us_link)
3792 printf("Free slabs:\n");
3793 LIST_FOREACH(slab, &dom->ud_free_slab, us_link)
3795 printf("Full slabs:\n");
3796 LIST_FOREACH(slab, &dom->ud_full_slab, us_link)
3802 uma_print_zone(uma_zone_t zone)
3807 printf("zone: %s(%p) size %d maxitems %ju flags %#x\n",
3808 zone->uz_name, zone, zone->uz_size, (uintmax_t)zone->uz_max_items,
3810 if (zone->uz_lockptr != &zone->uz_lock)
3811 uma_print_keg(zone->uz_keg);
3813 cache = &zone->uz_cpu[i];
3814 printf("CPU %d Cache:\n", i);
3821 * Generate statistics across both the zone and its per-cpu cache's. Return
3822 * desired statistics if the pointer is non-NULL for that statistic.
3824 * Note: does not update the zone statistics, as it can't safely clear the
3825 * per-CPU cache statistic.
3827 * XXXRW: Following the uc_allocbucket and uc_freebucket pointers here isn't
3828 * safe from off-CPU; we should modify the caches to track this information
3829 * directly so that we don't have to.
3832 uma_zone_sumstat(uma_zone_t z, long *cachefreep, uint64_t *allocsp,
3833 uint64_t *freesp, uint64_t *sleepsp)
3836 uint64_t allocs, frees, sleeps;
3839 allocs = frees = sleeps = 0;
3842 cache = &z->uz_cpu[cpu];
3843 if (cache->uc_allocbucket != NULL)
3844 cachefree += cache->uc_allocbucket->ub_cnt;
3845 if (cache->uc_freebucket != NULL)
3846 cachefree += cache->uc_freebucket->ub_cnt;
3847 allocs += cache->uc_allocs;
3848 frees += cache->uc_frees;
3850 allocs += counter_u64_fetch(z->uz_allocs);
3851 frees += counter_u64_fetch(z->uz_frees);
3852 sleeps += z->uz_sleeps;
3853 if (cachefreep != NULL)
3854 *cachefreep = cachefree;
3855 if (allocsp != NULL)
3859 if (sleepsp != NULL)
3865 sysctl_vm_zone_count(SYSCTL_HANDLER_ARGS)
3872 rw_rlock(&uma_rwlock);
3873 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3874 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3877 LIST_FOREACH(z, &uma_cachezones, uz_link)
3880 rw_runlock(&uma_rwlock);
3881 return (sysctl_handle_int(oidp, &count, 0, req));
3885 uma_vm_zone_stats(struct uma_type_header *uth, uma_zone_t z, struct sbuf *sbuf,
3886 struct uma_percpu_stat *ups, bool internal)
3888 uma_zone_domain_t zdom;
3893 for (i = 0; i < vm_ndomains; i++) {
3894 zdom = &z->uz_domain[i];
3895 uth->uth_zone_free += zdom->uzd_nitems;
3897 uth->uth_allocs = counter_u64_fetch(z->uz_allocs);
3898 uth->uth_frees = counter_u64_fetch(z->uz_frees);
3899 uth->uth_fails = counter_u64_fetch(z->uz_fails);
3900 uth->uth_sleeps = z->uz_sleeps;
3902 * While it is not normally safe to access the cache
3903 * bucket pointers while not on the CPU that owns the
3904 * cache, we only allow the pointers to be exchanged
3905 * without the zone lock held, not invalidated, so
3906 * accept the possible race associated with bucket
3907 * exchange during monitoring.
3909 for (i = 0; i < mp_maxid + 1; i++) {
3910 bzero(&ups[i], sizeof(*ups));
3911 if (internal || CPU_ABSENT(i))
3913 cache = &z->uz_cpu[i];
3914 if (cache->uc_allocbucket != NULL)
3915 ups[i].ups_cache_free +=
3916 cache->uc_allocbucket->ub_cnt;
3917 if (cache->uc_freebucket != NULL)
3918 ups[i].ups_cache_free +=
3919 cache->uc_freebucket->ub_cnt;
3920 ups[i].ups_allocs = cache->uc_allocs;
3921 ups[i].ups_frees = cache->uc_frees;
3926 sysctl_vm_zone_stats(SYSCTL_HANDLER_ARGS)
3928 struct uma_stream_header ush;
3929 struct uma_type_header uth;
3930 struct uma_percpu_stat *ups;
3934 int count, error, i;
3936 error = sysctl_wire_old_buffer(req, 0);
3939 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
3940 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
3941 ups = malloc((mp_maxid + 1) * sizeof(*ups), M_TEMP, M_WAITOK);
3944 rw_rlock(&uma_rwlock);
3945 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3946 LIST_FOREACH(z, &kz->uk_zones, uz_link)
3950 LIST_FOREACH(z, &uma_cachezones, uz_link)
3954 * Insert stream header.
3956 bzero(&ush, sizeof(ush));
3957 ush.ush_version = UMA_STREAM_VERSION;
3958 ush.ush_maxcpus = (mp_maxid + 1);
3959 ush.ush_count = count;
3960 (void)sbuf_bcat(&sbuf, &ush, sizeof(ush));
3962 LIST_FOREACH(kz, &uma_kegs, uk_link) {
3963 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
3964 bzero(&uth, sizeof(uth));
3966 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3967 uth.uth_align = kz->uk_align;
3968 uth.uth_size = kz->uk_size;
3969 uth.uth_rsize = kz->uk_rsize;
3970 if (z->uz_max_items > 0)
3971 uth.uth_pages = (z->uz_items / kz->uk_ipers) *
3974 uth.uth_pages = kz->uk_pages;
3975 uth.uth_maxpages = (z->uz_max_items / kz->uk_ipers) *
3977 uth.uth_limit = z->uz_max_items;
3978 uth.uth_keg_free = z->uz_keg->uk_free;
3981 * A zone is secondary is it is not the first entry
3982 * on the keg's zone list.
3984 if ((z->uz_flags & UMA_ZONE_SECONDARY) &&
3985 (LIST_FIRST(&kz->uk_zones) != z))
3986 uth.uth_zone_flags = UTH_ZONE_SECONDARY;
3987 uma_vm_zone_stats(&uth, z, &sbuf, ups,
3988 kz->uk_flags & UMA_ZFLAG_INTERNAL);
3990 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
3991 for (i = 0; i < mp_maxid + 1; i++)
3992 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
3995 LIST_FOREACH(z, &uma_cachezones, uz_link) {
3996 bzero(&uth, sizeof(uth));
3998 strlcpy(uth.uth_name, z->uz_name, UTH_MAX_NAME);
3999 uth.uth_size = z->uz_size;
4000 uma_vm_zone_stats(&uth, z, &sbuf, ups, false);
4002 (void)sbuf_bcat(&sbuf, &uth, sizeof(uth));
4003 for (i = 0; i < mp_maxid + 1; i++)
4004 (void)sbuf_bcat(&sbuf, &ups[i], sizeof(ups[i]));
4007 rw_runlock(&uma_rwlock);
4008 error = sbuf_finish(&sbuf);
4015 sysctl_handle_uma_zone_max(SYSCTL_HANDLER_ARGS)
4017 uma_zone_t zone = *(uma_zone_t *)arg1;
4020 max = uma_zone_get_max(zone);
4021 error = sysctl_handle_int(oidp, &max, 0, req);
4022 if (error || !req->newptr)
4025 uma_zone_set_max(zone, max);
4031 sysctl_handle_uma_zone_cur(SYSCTL_HANDLER_ARGS)
4033 uma_zone_t zone = *(uma_zone_t *)arg1;
4036 cur = uma_zone_get_cur(zone);
4037 return (sysctl_handle_int(oidp, &cur, 0, req));
4042 uma_dbg_getslab(uma_zone_t zone, void *item)
4048 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
4049 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
4050 slab = vtoslab((vm_offset_t)mem);
4053 * It is safe to return the slab here even though the
4054 * zone is unlocked because the item's allocation state
4055 * essentially holds a reference.
4057 if (zone->uz_lockptr == &zone->uz_lock)
4061 if (keg->uk_flags & UMA_ZONE_HASH)
4062 slab = hash_sfind(&keg->uk_hash, mem);
4064 slab = (uma_slab_t)(mem + keg->uk_pgoff);
4072 uma_dbg_zskip(uma_zone_t zone, void *mem)
4075 if (zone->uz_lockptr == &zone->uz_lock)
4078 return (uma_dbg_kskip(zone->uz_keg, mem));
4082 uma_dbg_kskip(uma_keg_t keg, void *mem)
4086 if (dbg_divisor == 0)
4089 if (dbg_divisor == 1)
4092 idx = (uintptr_t)mem >> PAGE_SHIFT;
4093 if (keg->uk_ipers > 1) {
4094 idx *= keg->uk_ipers;
4095 idx += ((uintptr_t)mem & PAGE_MASK) / keg->uk_rsize;
4098 if ((idx / dbg_divisor) * dbg_divisor != idx) {
4099 counter_u64_add(uma_skip_cnt, 1);
4102 counter_u64_add(uma_dbg_cnt, 1);
4108 * Set up the slab's freei data such that uma_dbg_free can function.
4112 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
4118 slab = uma_dbg_getslab(zone, item);
4120 panic("uma: item %p did not belong to zone %s\n",
4121 item, zone->uz_name);
4124 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4126 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4127 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
4128 item, zone, zone->uz_name, slab, freei);
4129 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4135 * Verifies freed addresses. Checks for alignment, valid slab membership
4136 * and duplicate frees.
4140 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
4146 slab = uma_dbg_getslab(zone, item);
4148 panic("uma: Freed item %p did not belong to zone %s\n",
4149 item, zone->uz_name);
4152 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
4154 if (freei >= keg->uk_ipers)
4155 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
4156 item, zone, zone->uz_name, slab, freei);
4158 if (((freei * keg->uk_rsize) + slab->us_data) != item)
4159 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
4160 item, zone, zone->uz_name, slab, freei);
4162 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
4163 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
4164 item, zone, zone->uz_name, slab, freei);
4166 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
4168 #endif /* INVARIANTS */
4171 DB_SHOW_COMMAND(uma, db_show_uma)
4175 uint64_t allocs, frees, sleeps;
4179 db_printf("%18s %8s %8s %8s %12s %8s %8s\n", "Zone", "Size", "Used",
4180 "Free", "Requests", "Sleeps", "Bucket");
4181 LIST_FOREACH(kz, &uma_kegs, uk_link) {
4182 LIST_FOREACH(z, &kz->uk_zones, uz_link) {
4183 if (kz->uk_flags & UMA_ZFLAG_INTERNAL) {
4184 allocs = counter_u64_fetch(z->uz_allocs);
4185 frees = counter_u64_fetch(z->uz_frees);
4186 sleeps = z->uz_sleeps;
4189 uma_zone_sumstat(z, &cachefree, &allocs,
4191 if (!((z->uz_flags & UMA_ZONE_SECONDARY) &&
4192 (LIST_FIRST(&kz->uk_zones) != z)))
4193 cachefree += kz->uk_free;
4194 for (i = 0; i < vm_ndomains; i++)
4195 cachefree += z->uz_domain[i].uzd_nitems;
4197 db_printf("%18s %8ju %8jd %8ld %12ju %8ju %8u\n",
4198 z->uz_name, (uintmax_t)kz->uk_size,
4199 (intmax_t)(allocs - frees), cachefree,
4200 (uintmax_t)allocs, sleeps, z->uz_count);
4207 DB_SHOW_COMMAND(umacache, db_show_umacache)
4210 uint64_t allocs, frees;
4214 db_printf("%18s %8s %8s %8s %12s %8s\n", "Zone", "Size", "Used", "Free",
4215 "Requests", "Bucket");
4216 LIST_FOREACH(z, &uma_cachezones, uz_link) {
4217 uma_zone_sumstat(z, &cachefree, &allocs, &frees, NULL);
4218 for (i = 0; i < vm_ndomains; i++)
4219 cachefree += z->uz_domain[i].uzd_nitems;
4220 db_printf("%18s %8ju %8jd %8ld %12ju %8u\n",
4221 z->uz_name, (uintmax_t)z->uz_size,
4222 (intmax_t)(allocs - frees), cachefree,
4223 (uintmax_t)allocs, z->uz_count);