2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1991, 1993
5 * The Regents of the University of California.
6 * Copyright (c) 2005-2009 Robert N. M. Watson
7 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> (mallocarray)
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
38 * Kernel malloc(9) implementation -- general purpose kernel memory allocator
39 * based on memory types. Back end is implemented using the UMA(9) zone
40 * allocator. A set of fixed-size buckets are used for smaller allocations,
41 * and a special UMA allocation interface is used for larger allocations.
42 * Callers declare memory types, and statistics are maintained independently
43 * for each memory type. Statistics are maintained per-CPU for performance
44 * reasons. See malloc(9) and comments in malloc.h for a detailed
48 #include <sys/cdefs.h>
52 #include <sys/param.h>
53 #include <sys/systm.h>
56 #include <sys/kernel.h>
58 #include <sys/malloc.h>
60 #include <sys/mutex.h>
61 #include <sys/vmmeter.h>
63 #include <sys/queue.h>
66 #include <sys/sysctl.h>
70 #include <sys/epoch.h>
75 #include <vm/vm_domainset.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_param.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_phys.h>
83 #include <vm/vm_pagequeue.h>
85 #include <vm/uma_int.h>
86 #include <vm/uma_dbg.h>
89 #include <vm/memguard.h>
92 #include <vm/redzone.h>
95 #if defined(INVARIANTS) && defined(__i386__)
96 #include <machine/cpu.h>
102 #include <sys/dtrace_bsd.h>
104 bool __read_frequently dtrace_malloc_enabled;
105 dtrace_malloc_probe_func_t __read_mostly dtrace_malloc_probe;
108 #if defined(INVARIANTS) || defined(MALLOC_MAKE_FAILURES) || \
109 defined(DEBUG_MEMGUARD) || defined(DEBUG_REDZONE)
110 #define MALLOC_DEBUG 1
113 #if defined(KASAN) || defined(DEBUG_REDZONE)
114 #define DEBUG_REDZONE_ARG_DEF , unsigned long osize
115 #define DEBUG_REDZONE_ARG , osize
117 #define DEBUG_REDZONE_ARG_DEF
118 #define DEBUG_REDZONE_ARG
122 * When realloc() is called, if the new size is sufficiently smaller than
123 * the old size, realloc() will allocate a new, smaller block to avoid
124 * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
125 * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
127 #ifndef REALLOC_FRACTION
128 #define REALLOC_FRACTION 1 /* new block if <= half the size */
132 * Centrally define some common malloc types.
134 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
135 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
136 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
138 static struct malloc_type *kmemstatistics;
139 static int kmemcount;
141 #define KMEM_ZSHIFT 4
142 #define KMEM_ZBASE 16
143 #define KMEM_ZMASK (KMEM_ZBASE - 1)
145 #define KMEM_ZMAX 65536
146 #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT)
147 static uint8_t kmemsize[KMEM_ZSIZE + 1];
149 #ifndef MALLOC_DEBUG_MAXZONES
150 #define MALLOC_DEBUG_MAXZONES 1
152 static int numzones = MALLOC_DEBUG_MAXZONES;
155 * Small malloc(9) memory allocations are allocated from a set of UMA buckets
158 * Warning: the layout of the struct is duplicated in libmemstat for KVM support.
160 * XXX: The comment here used to read "These won't be powers of two for
161 * long." It's possible that a significant amount of wasted memory could be
162 * recovered by tuning the sizes of these buckets.
167 uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES];
172 {128, "malloc-128", },
173 {256, "malloc-256", },
174 {384, "malloc-384", },
175 {512, "malloc-512", },
176 {1024, "malloc-1024", },
177 {2048, "malloc-2048", },
178 {4096, "malloc-4096", },
179 {8192, "malloc-8192", },
180 {16384, "malloc-16384", },
181 {32768, "malloc-32768", },
182 {65536, "malloc-65536", },
187 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0,
188 "Size of kernel memory");
190 static u_long kmem_zmax = KMEM_ZMAX;
191 SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0,
192 "Maximum allocation size that malloc(9) would use UMA as backend");
194 static u_long vm_kmem_size_min;
195 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0,
196 "Minimum size of kernel memory");
198 static u_long vm_kmem_size_max;
199 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0,
200 "Maximum size of kernel memory");
202 static u_int vm_kmem_size_scale;
203 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0,
204 "Scale factor for kernel memory size");
206 static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS);
207 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size,
208 CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
209 sysctl_kmem_map_size, "LU", "Current kmem allocation size");
211 static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS);
212 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free,
213 CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
214 sysctl_kmem_map_free, "LU", "Free space in kmem");
216 static SYSCTL_NODE(_vm, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
217 "Malloc information");
219 static u_int vm_malloc_zone_count = nitems(kmemzones);
220 SYSCTL_UINT(_vm_malloc, OID_AUTO, zone_count,
221 CTLFLAG_RD, &vm_malloc_zone_count, 0,
222 "Number of malloc zones");
224 static int sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS);
225 SYSCTL_PROC(_vm_malloc, OID_AUTO, zone_sizes,
226 CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE, NULL, 0,
227 sysctl_vm_malloc_zone_sizes, "S", "Zone sizes used by malloc");
230 * The malloc_mtx protects the kmemstatistics linked list.
232 struct mtx malloc_mtx;
234 static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
236 #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1)
237 static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
238 "Kernel malloc debugging options");
242 * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
243 * the caller specifies M_NOWAIT. If set to 0, no failures are caused.
245 #ifdef MALLOC_MAKE_FAILURES
246 static int malloc_failure_rate;
247 static int malloc_nowait_count;
248 static int malloc_failure_count;
249 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN,
250 &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
251 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
252 &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
256 sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS)
261 return (sysctl_handle_long(oidp, &size, 0, req));
265 sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS)
269 /* The sysctl is unsigned, implement as a saturation value. */
276 return (sysctl_handle_long(oidp, &size, 0, req));
280 sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS)
282 int sizes[nitems(kmemzones)];
285 for (i = 0; i < nitems(kmemzones); i++) {
286 sizes[i] = kmemzones[i].kz_size;
289 return (SYSCTL_OUT(req, &sizes, sizeof(sizes)));
293 * malloc(9) uma zone separation -- sub-page buffer overruns in one
294 * malloc type will affect only a subset of other malloc types.
296 #if MALLOC_DEBUG_MAXZONES > 1
298 tunable_set_numzones(void)
301 TUNABLE_INT_FETCH("debug.malloc.numzones",
304 /* Sanity check the number of malloc uma zones. */
307 if (numzones > MALLOC_DEBUG_MAXZONES)
308 numzones = MALLOC_DEBUG_MAXZONES;
310 SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL);
311 SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
312 &numzones, 0, "Number of malloc uma subzones");
315 * Any number that changes regularly is an okay choice for the
316 * offset. Build numbers are pretty good of you have them.
318 static u_int zone_offset = __FreeBSD_version;
319 TUNABLE_INT("debug.malloc.zone_offset", &zone_offset);
320 SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN,
321 &zone_offset, 0, "Separate malloc types by examining the "
322 "Nth character in the malloc type short description.");
325 mtp_set_subzone(struct malloc_type *mtp)
327 struct malloc_type_internal *mtip;
333 desc = mtp->ks_shortdesc;
334 if (desc == NULL || (len = strlen(desc)) == 0)
337 val = desc[zone_offset % len];
338 mtip->mti_zone = (val % numzones);
342 mtp_get_subzone(struct malloc_type *mtp)
344 struct malloc_type_internal *mtip;
348 KASSERT(mtip->mti_zone < numzones,
349 ("mti_zone %u out of range %d",
350 mtip->mti_zone, numzones));
351 return (mtip->mti_zone);
353 #elif MALLOC_DEBUG_MAXZONES == 0
354 #error "MALLOC_DEBUG_MAXZONES must be positive."
357 mtp_set_subzone(struct malloc_type *mtp)
359 struct malloc_type_internal *mtip;
366 mtp_get_subzone(struct malloc_type *mtp)
371 #endif /* MALLOC_DEBUG_MAXZONES > 1 */
374 * An allocation has succeeded -- update malloc type statistics for the
375 * amount of bucket size. Occurs within a critical section so that the
376 * thread isn't preempted and doesn't migrate while updating per-PCU
380 malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
383 struct malloc_type_internal *mtip;
384 struct malloc_type_stats *mtsp;
388 mtsp = zpcpu_get(mtip->mti_stats);
390 mtsp->mts_memalloced += size;
391 mtsp->mts_numallocs++;
394 mtsp->mts_size |= 1 << zindx;
397 if (__predict_false(dtrace_malloc_enabled)) {
398 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC];
400 (dtrace_malloc_probe)(probe_id,
401 (uintptr_t) mtp, (uintptr_t) mtip,
402 (uintptr_t) mtsp, size, zindx);
410 malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
414 malloc_type_zone_allocated(mtp, size, -1);
418 * A free operation has occurred -- update malloc type statistics for the
419 * amount of the bucket size. Occurs within a critical section so that the
420 * thread isn't preempted and doesn't migrate while updating per-CPU
424 malloc_type_freed(struct malloc_type *mtp, unsigned long size)
426 struct malloc_type_internal *mtip;
427 struct malloc_type_stats *mtsp;
431 mtsp = zpcpu_get(mtip->mti_stats);
432 mtsp->mts_memfreed += size;
433 mtsp->mts_numfrees++;
436 if (__predict_false(dtrace_malloc_enabled)) {
437 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE];
439 (dtrace_malloc_probe)(probe_id,
440 (uintptr_t) mtp, (uintptr_t) mtip,
441 (uintptr_t) mtsp, size, 0);
451 * Allocate a block of physically contiguous memory.
453 * If M_NOWAIT is set, this routine will not block and return NULL if
454 * the allocation fails.
457 contigmalloc(unsigned long size, struct malloc_type *type, int flags,
458 vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
463 ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment,
464 boundary, VM_MEMATTR_DEFAULT);
466 malloc_type_allocated(type, round_page(size));
471 contigmalloc_domainset(unsigned long size, struct malloc_type *type,
472 struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high,
473 unsigned long alignment, vm_paddr_t boundary)
477 ret = (void *)kmem_alloc_contig_domainset(ds, size, flags, low, high,
478 alignment, boundary, VM_MEMATTR_DEFAULT);
480 malloc_type_allocated(type, round_page(size));
487 * Free a block of memory allocated by contigmalloc.
489 * This routine may not block.
492 contigfree(void *addr, unsigned long size, struct malloc_type *type)
495 kmem_free(addr, size);
496 malloc_type_freed(type, round_page(size));
501 malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp,
507 KASSERT(mtp->ks_version == M_VERSION, ("malloc: bad malloc type version"));
509 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
511 indx = flags & (M_WAITOK | M_NOWAIT);
512 if (indx != M_NOWAIT && indx != M_WAITOK) {
513 static struct timeval lasterr;
514 static int curerr, once;
515 if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
516 printf("Bad malloc flags: %x\n", indx);
523 #ifdef MALLOC_MAKE_FAILURES
524 if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
525 atomic_add_int(&malloc_nowait_count, 1);
526 if ((malloc_nowait_count % malloc_failure_rate) == 0) {
527 atomic_add_int(&malloc_failure_count, 1);
529 return (EJUSTRETURN);
533 if (flags & M_WAITOK) {
534 KASSERT(curthread->td_intr_nesting_level == 0,
535 ("malloc(M_WAITOK) in interrupt context"));
536 if (__predict_false(!THREAD_CAN_SLEEP())) {
538 epoch_trace_list(curthread);
541 ("malloc(M_WAITOK) with sleeping prohibited"));
544 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
545 ("malloc: called with spinlock or critical section held"));
547 #ifdef DEBUG_MEMGUARD
548 if (memguard_cmp_mtp(mtp, *sizep)) {
549 *vap = memguard_alloc(*sizep, flags);
551 return (EJUSTRETURN);
552 /* This is unfortunate but should not be fatal. */
557 *sizep = redzone_size_ntor(*sizep);
565 * Handle large allocations and frees by using kmem_malloc directly.
568 malloc_large_slab(uma_slab_t slab)
572 va = (uintptr_t)slab;
573 return ((va & 1) != 0);
577 malloc_large_size(uma_slab_t slab)
581 va = (uintptr_t)slab;
585 static caddr_t __noinline
586 malloc_large(size_t size, struct malloc_type *mtp, struct domainset *policy,
587 int flags DEBUG_REDZONE_ARG_DEF)
591 size = roundup(size, PAGE_SIZE);
592 va = kmem_malloc_domainset(policy, size, flags);
594 /* The low bit is unused for slab pointers. */
595 vsetzoneslab((uintptr_t)va, NULL, (void *)((size << 1) | 1));
598 malloc_type_allocated(mtp, va == NULL ? 0 : size);
599 if (__predict_false(va == NULL)) {
600 KASSERT((flags & M_WAITOK) == 0,
601 ("malloc(M_WAITOK) returned NULL"));
604 va = redzone_setup(va, osize);
606 kasan_mark(va, osize, size, KASAN_MALLOC_REDZONE);
612 free_large(void *addr, size_t size)
615 kmem_free(addr, size);
622 * Allocate a block of memory.
624 * If M_NOWAIT is set, this routine will not block and return NULL if
625 * the allocation fails.
628 (malloc)(size_t size, struct malloc_type *mtp, int flags)
633 #if defined(DEBUG_REDZONE) || defined(KASAN)
634 unsigned long osize = size;
637 MPASS((flags & M_EXEC) == 0);
641 if (malloc_dbg(&va, &size, mtp, flags) != 0)
645 if (__predict_false(size > kmem_zmax))
646 return (malloc_large(size, mtp, DOMAINSET_RR(), flags
649 if (size & KMEM_ZMASK)
650 size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
651 indx = kmemsize[size >> KMEM_ZSHIFT];
652 zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)];
653 va = uma_zalloc(zone, flags);
655 size = zone->uz_size;
656 if ((flags & M_ZERO) == 0) {
657 kmsan_mark(va, size, KMSAN_STATE_UNINIT);
658 kmsan_orig(va, size, KMSAN_TYPE_MALLOC, KMSAN_RET_ADDR);
661 malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
662 if (__predict_false(va == NULL)) {
663 KASSERT((flags & M_WAITOK) == 0,
664 ("malloc(M_WAITOK) returned NULL"));
668 va = redzone_setup(va, osize);
672 kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
674 return ((void *) va);
678 malloc_domain(size_t *sizep, int *indxp, struct malloc_type *mtp, int domain,
687 KASSERT(size <= kmem_zmax && (flags & M_EXEC) == 0,
688 ("malloc_domain: Called with bad flag / size combination."));
689 if (size & KMEM_ZMASK)
690 size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
691 indx = kmemsize[size >> KMEM_ZSHIFT];
692 zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)];
693 va = uma_zalloc_domain(zone, NULL, domain, flags);
695 *sizep = zone->uz_size;
701 malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds,
704 struct vm_domainset_iter di;
708 #if defined(KASAN) || defined(DEBUG_REDZONE)
709 unsigned long osize = size;
712 MPASS((flags & M_EXEC) == 0);
716 if (malloc_dbg(&va, &size, mtp, flags) != 0)
720 if (__predict_false(size > kmem_zmax))
721 return (malloc_large(size, mtp, DOMAINSET_RR(), flags
724 vm_domainset_iter_policy_init(&di, ds, &domain, &flags);
726 va = malloc_domain(&size, &indx, mtp, domain, flags);
727 } while (va == NULL && vm_domainset_iter_policy(&di, &domain) == 0);
728 malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
729 if (__predict_false(va == NULL)) {
730 KASSERT((flags & M_WAITOK) == 0,
731 ("malloc(M_WAITOK) returned NULL"));
735 va = redzone_setup(va, osize);
739 kasan_mark((void *)va, osize, size, KASAN_MALLOC_REDZONE);
742 if ((flags & M_ZERO) == 0) {
743 kmsan_mark(va, size, KMSAN_STATE_UNINIT);
744 kmsan_orig(va, size, KMSAN_TYPE_MALLOC, KMSAN_RET_ADDR);
751 * Allocate an executable area.
754 malloc_exec(size_t size, struct malloc_type *mtp, int flags)
757 return (malloc_domainset_exec(size, mtp, DOMAINSET_RR(), flags));
761 malloc_domainset_exec(size_t size, struct malloc_type *mtp, struct domainset *ds,
764 #if defined(DEBUG_REDZONE) || defined(KASAN)
765 unsigned long osize = size;
775 if (malloc_dbg(&va, &size, mtp, flags) != 0)
779 return (malloc_large(size, mtp, ds, flags DEBUG_REDZONE_ARG));
783 malloc_aligned(size_t size, size_t align, struct malloc_type *type, int flags)
785 return (malloc_domainset_aligned(size, align, type, DOMAINSET_RR(),
790 malloc_domainset_aligned(size_t size, size_t align,
791 struct malloc_type *mtp, struct domainset *ds, int flags)
796 KASSERT(powerof2(align),
797 ("malloc_domainset_aligned: wrong align %#zx size %#zx",
799 KASSERT(align <= PAGE_SIZE,
800 ("malloc_domainset_aligned: align %#zx (size %#zx) too large",
804 * Round the allocation size up to the next power of 2,
805 * because we can only guarantee alignment for
806 * power-of-2-sized allocations. Further increase the
807 * allocation size to align if the rounded size is less than
808 * align, since malloc zones provide alignment equal to their
813 asize = size <= align ? align : 1UL << flsl(size - 1);
815 res = malloc_domainset(asize, mtp, ds, flags);
816 KASSERT(res == NULL || ((uintptr_t)res & (align - 1)) == 0,
817 ("malloc_domainset_aligned: result not aligned %p size %#zx "
818 "allocsize %#zx align %#zx", res, size, asize, align));
823 mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags)
826 if (WOULD_OVERFLOW(nmemb, size))
827 panic("mallocarray: %zu * %zu overflowed", nmemb, size);
829 return (malloc(size * nmemb, type, flags));
833 mallocarray_domainset(size_t nmemb, size_t size, struct malloc_type *type,
834 struct domainset *ds, int flags)
837 if (WOULD_OVERFLOW(nmemb, size))
838 panic("mallocarray_domainset: %zu * %zu overflowed", nmemb, size);
840 return (malloc_domainset(size * nmemb, type, ds, flags));
843 #if defined(INVARIANTS) && !defined(KASAN)
845 free_save_type(void *addr, struct malloc_type *mtp, u_long size)
847 struct malloc_type **mtpp = addr;
850 * Cache a pointer to the malloc_type that most recently freed
851 * this memory here. This way we know who is most likely to
852 * have stepped on it later.
854 * This code assumes that size is a multiple of 8 bytes for
857 mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
858 mtpp += (size - sizeof(struct malloc_type *)) /
859 sizeof(struct malloc_type *);
866 free_dbg(void **addrp, struct malloc_type *mtp)
871 KASSERT(mtp->ks_version == M_VERSION, ("free: bad malloc type version"));
872 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
873 ("free: called with spinlock or critical section held"));
875 /* free(NULL, ...) does nothing */
877 return (EJUSTRETURN);
879 #ifdef DEBUG_MEMGUARD
880 if (is_memguard_addr(addr)) {
882 return (EJUSTRETURN);
888 *addrp = redzone_addr_ntor(addr);
898 * Free a block of memory allocated by malloc.
900 * This routine may not block.
903 free(void *addr, struct malloc_type *mtp)
910 if (free_dbg(&addr, mtp) != 0)
913 /* free(NULL, ...) does nothing */
917 vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
919 panic("free: address %p(%p) has not been allocated.\n",
920 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
922 if (__predict_true(!malloc_large_slab(slab))) {
923 size = zone->uz_size;
924 #if defined(INVARIANTS) && !defined(KASAN)
925 free_save_type(addr, mtp, size);
927 uma_zfree_arg(zone, addr, slab);
929 size = malloc_large_size(slab);
930 free_large(addr, size);
932 malloc_type_freed(mtp, size);
938 * Zero then free a block of memory allocated by malloc.
940 * This routine may not block.
943 zfree(void *addr, struct malloc_type *mtp)
950 if (free_dbg(&addr, mtp) != 0)
953 /* free(NULL, ...) does nothing */
957 vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
959 panic("free: address %p(%p) has not been allocated.\n",
960 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
962 if (__predict_true(!malloc_large_slab(slab))) {
963 size = zone->uz_size;
964 #if defined(INVARIANTS) && !defined(KASAN)
965 free_save_type(addr, mtp, size);
967 kasan_mark(addr, size, size, 0);
968 explicit_bzero(addr, size);
969 uma_zfree_arg(zone, addr, slab);
971 size = malloc_large_size(slab);
972 kasan_mark(addr, size, size, 0);
973 explicit_bzero(addr, size);
974 free_large(addr, size);
976 malloc_type_freed(mtp, size);
980 * realloc: change the size of a memory block
983 realloc(void *addr, size_t size, struct malloc_type *mtp, int flags)
985 #ifndef DEBUG_REDZONE
992 KASSERT(mtp->ks_version == M_VERSION,
993 ("realloc: bad malloc type version"));
994 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
995 ("realloc: called with spinlock or critical section held"));
997 /* realloc(NULL, ...) is equivalent to malloc(...) */
999 return (malloc(size, mtp, flags));
1002 * XXX: Should report free of old memory and alloc of new memory to
1006 #ifdef DEBUG_MEMGUARD
1007 if (is_memguard_addr(addr))
1008 return (memguard_realloc(addr, size, mtp, flags));
1011 #ifdef DEBUG_REDZONE
1012 alloc = redzone_get_size(addr);
1014 vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
1017 KASSERT(slab != NULL,
1018 ("realloc: address %p out of range", (void *)addr));
1020 /* Get the size of the original block */
1021 if (!malloc_large_slab(slab))
1022 alloc = zone->uz_size;
1024 alloc = malloc_large_size(slab);
1026 /* Reuse the original block if appropriate */
1027 if (size <= alloc &&
1028 (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) {
1029 kasan_mark((void *)addr, size, alloc, KASAN_MALLOC_REDZONE);
1032 #endif /* !DEBUG_REDZONE */
1034 /* Allocate a new, bigger (or smaller) block */
1035 if ((newaddr = malloc(size, mtp, flags)) == NULL)
1039 * Copy over original contents. For KASAN, the redzone must be marked
1040 * valid before performing the copy.
1042 kasan_mark(addr, alloc, alloc, 0);
1043 bcopy(addr, newaddr, min(size, alloc));
1049 * reallocf: same as realloc() but free memory on failure.
1052 reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags)
1056 if ((mem = realloc(addr, size, mtp, flags)) == NULL)
1062 * malloc_size: returns the number of bytes allocated for a request of the
1066 malloc_size(size_t size)
1070 if (size > kmem_zmax)
1072 if (size & KMEM_ZMASK)
1073 size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
1074 indx = kmemsize[size >> KMEM_ZSHIFT];
1075 return (kmemzones[indx].kz_size);
1079 * malloc_usable_size: returns the usable size of the allocation.
1082 malloc_usable_size(const void *addr)
1084 #ifndef DEBUG_REDZONE
1093 #ifdef DEBUG_MEMGUARD
1094 if (is_memguard_addr(__DECONST(void *, addr)))
1095 return (memguard_get_req_size(addr));
1098 #ifdef DEBUG_REDZONE
1099 size = redzone_get_size(__DECONST(void *, addr));
1101 vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab);
1103 panic("malloc_usable_size: address %p(%p) is not allocated.\n",
1104 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
1106 if (!malloc_large_slab(slab))
1107 size = zone->uz_size;
1109 size = malloc_large_size(slab);
1113 * Unmark the redzone to avoid reports from consumers who are
1114 * (presumably) about to use the full allocation size.
1116 kasan_mark(addr, size, size, 0);
1121 CTASSERT(VM_KMEM_SIZE_SCALE >= 1);
1124 * Initialize the kernel memory (kmem) arena.
1133 if (vm_kmem_size == 0)
1134 vm_kmem_size = VM_KMEM_SIZE;
1136 #ifdef VM_KMEM_SIZE_MIN
1137 if (vm_kmem_size_min == 0)
1138 vm_kmem_size_min = VM_KMEM_SIZE_MIN;
1140 #ifdef VM_KMEM_SIZE_MAX
1141 if (vm_kmem_size_max == 0)
1142 vm_kmem_size_max = VM_KMEM_SIZE_MAX;
1145 * Calculate the amount of kernel virtual address (KVA) space that is
1146 * preallocated to the kmem arena. In order to support a wide range
1147 * of machines, it is a function of the physical memory size,
1150 * min(max(physical memory size / VM_KMEM_SIZE_SCALE,
1151 * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX)
1153 * Every architecture must define an integral value for
1154 * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN
1155 * and VM_KMEM_SIZE_MAX, which represent respectively the floor and
1156 * ceiling on this preallocation, are optional. Typically,
1157 * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on
1158 * a given architecture.
1160 mem_size = vm_cnt.v_page_count;
1161 if (mem_size <= 32768) /* delphij XXX 128MB */
1162 kmem_zmax = PAGE_SIZE;
1164 if (vm_kmem_size_scale < 1)
1165 vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
1168 * Check if we should use defaults for the "vm_kmem_size"
1171 if (vm_kmem_size == 0) {
1172 vm_kmem_size = mem_size / vm_kmem_size_scale;
1173 vm_kmem_size = vm_kmem_size * PAGE_SIZE < vm_kmem_size ?
1174 vm_kmem_size_max : vm_kmem_size * PAGE_SIZE;
1175 if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min)
1176 vm_kmem_size = vm_kmem_size_min;
1177 if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
1178 vm_kmem_size = vm_kmem_size_max;
1180 if (vm_kmem_size == 0)
1181 panic("Tune VM_KMEM_SIZE_* for the platform");
1184 * The amount of KVA space that is preallocated to the
1185 * kmem arena can be set statically at compile-time or manually
1186 * through the kernel environment. However, it is still limited to
1187 * twice the physical memory size, which has been sufficient to handle
1188 * the most severe cases of external fragmentation in the kmem arena.
1190 if (vm_kmem_size / 2 / PAGE_SIZE > mem_size)
1191 vm_kmem_size = 2 * mem_size * PAGE_SIZE;
1193 vm_kmem_size = round_page(vm_kmem_size);
1196 * With KASAN or KMSAN enabled, dynamically allocated kernel memory is
1197 * shadowed. Account for this when setting the UMA limit.
1200 vm_kmem_size = (vm_kmem_size * KASAN_SHADOW_SCALE) /
1201 (KASAN_SHADOW_SCALE + 1);
1202 #elif defined(KMSAN)
1206 #ifdef DEBUG_MEMGUARD
1207 tmp = memguard_fudge(vm_kmem_size, kernel_map);
1213 #ifdef DEBUG_MEMGUARD
1215 * Initialize MemGuard if support compiled in. MemGuard is a
1216 * replacement allocator used for detecting tamper-after-free
1217 * scenarios as they occur. It is only used for debugging.
1219 memguard_init(kernel_arena);
1224 * Initialize the kernel memory allocator
1228 mallocinit(void *dummy)
1233 mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
1237 if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX)
1238 kmem_zmax = KMEM_ZMAX;
1240 for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
1241 int size = kmemzones[indx].kz_size;
1242 const char *name = kmemzones[indx].kz_name;
1246 align = UMA_ALIGN_PTR;
1247 if (powerof2(size) && size > sizeof(void *))
1248 align = MIN(size, PAGE_SIZE) - 1;
1249 for (subzone = 0; subzone < numzones; subzone++) {
1250 kmemzones[indx].kz_zone[subzone] =
1251 uma_zcreate(name, size,
1252 #if defined(INVARIANTS) && !defined(KASAN) && !defined(KMSAN)
1253 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
1255 NULL, NULL, NULL, NULL,
1257 align, UMA_ZONE_MALLOC);
1259 for (;i <= size; i+= KMEM_ZBASE)
1260 kmemsize[i >> KMEM_ZSHIFT] = indx;
1263 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL);
1266 malloc_init(void *data)
1268 struct malloc_type_internal *mtip;
1269 struct malloc_type *mtp;
1271 KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init"));
1274 if (mtp->ks_version != M_VERSION)
1275 panic("malloc_init: type %s with unsupported version %lu",
1276 mtp->ks_shortdesc, mtp->ks_version);
1278 mtip = &mtp->ks_mti;
1279 mtip->mti_stats = uma_zalloc_pcpu(pcpu_zone_64, M_WAITOK | M_ZERO);
1280 mtp_set_subzone(mtp);
1282 mtx_lock(&malloc_mtx);
1283 mtp->ks_next = kmemstatistics;
1284 kmemstatistics = mtp;
1286 mtx_unlock(&malloc_mtx);
1290 malloc_uninit(void *data)
1292 struct malloc_type_internal *mtip;
1293 struct malloc_type_stats *mtsp;
1294 struct malloc_type *mtp, *temp;
1295 long temp_allocs, temp_bytes;
1299 KASSERT(mtp->ks_version == M_VERSION,
1300 ("malloc_uninit: bad malloc type version"));
1302 mtx_lock(&malloc_mtx);
1303 mtip = &mtp->ks_mti;
1304 if (mtp != kmemstatistics) {
1305 for (temp = kmemstatistics; temp != NULL;
1306 temp = temp->ks_next) {
1307 if (temp->ks_next == mtp) {
1308 temp->ks_next = mtp->ks_next;
1313 ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc));
1315 kmemstatistics = mtp->ks_next;
1317 mtx_unlock(&malloc_mtx);
1320 * Look for memory leaks.
1322 temp_allocs = temp_bytes = 0;
1323 for (i = 0; i <= mp_maxid; i++) {
1324 mtsp = zpcpu_get_cpu(mtip->mti_stats, i);
1325 temp_allocs += mtsp->mts_numallocs;
1326 temp_allocs -= mtsp->mts_numfrees;
1327 temp_bytes += mtsp->mts_memalloced;
1328 temp_bytes -= mtsp->mts_memfreed;
1330 if (temp_allocs > 0 || temp_bytes > 0) {
1331 printf("Warning: memory type %s leaked memory on destroy "
1332 "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
1333 temp_allocs, temp_bytes);
1336 uma_zfree_pcpu(pcpu_zone_64, mtip->mti_stats);
1339 struct malloc_type *
1340 malloc_desc2type(const char *desc)
1342 struct malloc_type *mtp;
1344 mtx_assert(&malloc_mtx, MA_OWNED);
1345 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1346 if (strcmp(mtp->ks_shortdesc, desc) == 0)
1353 sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
1355 struct malloc_type_stream_header mtsh;
1356 struct malloc_type_internal *mtip;
1357 struct malloc_type_stats *mtsp, zeromts;
1358 struct malloc_type_header mth;
1359 struct malloc_type *mtp;
1363 error = sysctl_wire_old_buffer(req, 0);
1366 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
1367 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
1368 mtx_lock(&malloc_mtx);
1370 bzero(&zeromts, sizeof(zeromts));
1373 * Insert stream header.
1375 bzero(&mtsh, sizeof(mtsh));
1376 mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
1377 mtsh.mtsh_maxcpus = MAXCPU;
1378 mtsh.mtsh_count = kmemcount;
1379 (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh));
1382 * Insert alternating sequence of type headers and type statistics.
1384 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1385 mtip = &mtp->ks_mti;
1388 * Insert type header.
1390 bzero(&mth, sizeof(mth));
1391 strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
1392 (void)sbuf_bcat(&sbuf, &mth, sizeof(mth));
1395 * Insert type statistics for each CPU.
1397 for (i = 0; i <= mp_maxid; i++) {
1398 mtsp = zpcpu_get_cpu(mtip->mti_stats, i);
1399 (void)sbuf_bcat(&sbuf, mtsp, sizeof(*mtsp));
1402 * Fill in the missing CPUs.
1404 for (; i < MAXCPU; i++) {
1405 (void)sbuf_bcat(&sbuf, &zeromts, sizeof(zeromts));
1408 mtx_unlock(&malloc_mtx);
1409 error = sbuf_finish(&sbuf);
1414 SYSCTL_PROC(_kern, OID_AUTO, malloc_stats,
1415 CTLFLAG_RD | CTLTYPE_STRUCT | CTLFLAG_MPSAFE, 0, 0,
1416 sysctl_kern_malloc_stats, "s,malloc_type_ustats",
1417 "Return malloc types");
1419 SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
1420 "Count of kernel malloc types");
1423 malloc_type_list(malloc_type_list_func_t *func, void *arg)
1425 struct malloc_type *mtp, **bufmtp;
1429 mtx_lock(&malloc_mtx);
1431 mtx_assert(&malloc_mtx, MA_OWNED);
1433 mtx_unlock(&malloc_mtx);
1435 buflen = sizeof(struct malloc_type *) * count;
1436 bufmtp = malloc(buflen, M_TEMP, M_WAITOK);
1438 mtx_lock(&malloc_mtx);
1440 if (count < kmemcount) {
1441 free(bufmtp, M_TEMP);
1445 for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++)
1448 mtx_unlock(&malloc_mtx);
1450 for (i = 0; i < count; i++)
1451 (func)(bufmtp[i], arg);
1453 free(bufmtp, M_TEMP);
1458 get_malloc_stats(const struct malloc_type_internal *mtip, uint64_t *allocs,
1461 const struct malloc_type_stats *mtsp;
1462 uint64_t frees, alloced, freed;
1469 for (i = 0; i <= mp_maxid; i++) {
1470 mtsp = zpcpu_get_cpu(mtip->mti_stats, i);
1472 *allocs += mtsp->mts_numallocs;
1473 frees += mtsp->mts_numfrees;
1474 alloced += mtsp->mts_memalloced;
1475 freed += mtsp->mts_memfreed;
1477 *inuse = *allocs - frees;
1478 return (alloced - freed);
1481 DB_SHOW_COMMAND_FLAGS(malloc, db_show_malloc, DB_CMD_MEMSAFE)
1483 const char *fmt_hdr, *fmt_entry;
1484 struct malloc_type *mtp;
1485 uint64_t allocs, inuse;
1487 /* variables for sorting */
1488 struct malloc_type *last_mtype, *cur_mtype;
1489 int64_t cur_size, last_size;
1492 if (modif[0] == 'i') {
1493 fmt_hdr = "%s,%s,%s,%s\n";
1494 fmt_entry = "\"%s\",%ju,%jdK,%ju\n";
1496 fmt_hdr = "%18s %12s %12s %12s\n";
1497 fmt_entry = "%18s %12ju %12jdK %12ju\n";
1500 db_printf(fmt_hdr, "Type", "InUse", "MemUse", "Requests");
1502 /* Select sort, largest size first. */
1504 last_size = INT64_MAX;
1510 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1512 * In the case of size ties, print out mtypes
1513 * in the order they are encountered. That is,
1514 * when we encounter the most recently output
1515 * mtype, we have already printed all preceding
1516 * ties, and we must print all following ties.
1518 if (mtp == last_mtype) {
1522 size = get_malloc_stats(&mtp->ks_mti, &allocs,
1524 if (size > cur_size && size < last_size + ties) {
1529 if (cur_mtype == NULL)
1532 size = get_malloc_stats(&cur_mtype->ks_mti, &allocs, &inuse);
1533 db_printf(fmt_entry, cur_mtype->ks_shortdesc, inuse,
1534 howmany(size, 1024), allocs);
1539 last_mtype = cur_mtype;
1540 last_size = cur_size;
1544 #if MALLOC_DEBUG_MAXZONES > 1
1545 DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches)
1547 struct malloc_type_internal *mtip;
1548 struct malloc_type *mtp;
1552 db_printf("Usage: show multizone_matches <malloc type/addr>\n");
1556 if (mtp->ks_version != M_VERSION) {
1557 db_printf("Version %lx does not match expected %x\n",
1558 mtp->ks_version, M_VERSION);
1562 mtip = &mtp->ks_mti;
1563 subzone = mtip->mti_zone;
1565 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1566 mtip = &mtp->ks_mti;
1567 if (mtip->mti_zone != subzone)
1569 db_printf("%s\n", mtp->ks_shortdesc);
1574 #endif /* MALLOC_DEBUG_MAXZONES > 1 */