2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2005-2009 Robert N. M. Watson
5 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> (mallocarray)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
36 * Kernel malloc(9) implementation -- general purpose kernel memory allocator
37 * based on memory types. Back end is implemented using the UMA(9) zone
38 * allocator. A set of fixed-size buckets are used for smaller allocations,
39 * and a special UMA allocation interface is used for larger allocations.
40 * Callers declare memory types, and statistics are maintained independently
41 * for each memory type. Statistics are maintained per-CPU for performance
42 * reasons. See malloc(9) and comments in malloc.h for a detailed
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
52 #include <sys/param.h>
53 #include <sys/systm.h>
55 #include <sys/kernel.h>
57 #include <sys/malloc.h>
58 #include <sys/mutex.h>
59 #include <sys/vmmeter.h>
62 #include <sys/sysctl.h>
68 #include <vm/vm_pageout.h>
69 #include <vm/vm_param.h>
70 #include <vm/vm_kern.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_page.h>
75 #include <vm/uma_int.h>
76 #include <vm/uma_dbg.h>
79 #include <vm/memguard.h>
82 #include <vm/redzone.h>
85 #if defined(INVARIANTS) && defined(__i386__)
86 #include <machine/cpu.h>
92 #include <sys/dtrace_bsd.h>
94 dtrace_malloc_probe_func_t dtrace_malloc_probe;
98 * When realloc() is called, if the new size is sufficiently smaller than
99 * the old size, realloc() will allocate a new, smaller block to avoid
100 * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
101 * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
103 #ifndef REALLOC_FRACTION
104 #define REALLOC_FRACTION 1 /* new block if <= half the size */
108 * Centrally define some common malloc types.
110 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
111 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
112 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
114 static struct malloc_type *kmemstatistics;
115 static int kmemcount;
117 #define KMEM_ZSHIFT 4
118 #define KMEM_ZBASE 16
119 #define KMEM_ZMASK (KMEM_ZBASE - 1)
121 #define KMEM_ZMAX 65536
122 #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT)
123 static uint8_t kmemsize[KMEM_ZSIZE + 1];
125 #ifndef MALLOC_DEBUG_MAXZONES
126 #define MALLOC_DEBUG_MAXZONES 1
128 static int numzones = MALLOC_DEBUG_MAXZONES;
131 * Small malloc(9) memory allocations are allocated from a set of UMA buckets
134 * XXX: The comment here used to read "These won't be powers of two for
135 * long." It's possible that a significant amount of wasted memory could be
136 * recovered by tuning the sizes of these buckets.
141 uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES];
160 * Zone to allocate malloc type descriptions from. For ABI reasons, memory
161 * types are described by a data structure passed by the declaring code, but
162 * the malloc(9) implementation has its own data structure describing the
163 * type and statistics. This permits the malloc(9)-internal data structures
164 * to be modified without breaking binary-compiled kernel modules that
165 * declare malloc types.
167 static uma_zone_t mt_zone;
170 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0,
171 "Size of kernel memory");
173 static u_long kmem_zmax = KMEM_ZMAX;
174 SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0,
175 "Maximum allocation size that malloc(9) would use UMA as backend");
177 static u_long vm_kmem_size_min;
178 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0,
179 "Minimum size of kernel memory");
181 static u_long vm_kmem_size_max;
182 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0,
183 "Maximum size of kernel memory");
185 static u_int vm_kmem_size_scale;
186 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0,
187 "Scale factor for kernel memory size");
189 static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS);
190 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size,
191 CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
192 sysctl_kmem_map_size, "LU", "Current kmem allocation size");
194 static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS);
195 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free,
196 CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
197 sysctl_kmem_map_free, "LU", "Free space in kmem");
200 * The malloc_mtx protects the kmemstatistics linked list.
202 struct mtx malloc_mtx;
204 #ifdef MALLOC_PROFILE
205 uint64_t krequests[KMEM_ZSIZE + 1];
207 static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
210 static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
213 * time_uptime of the last malloc(9) failure (induced or real).
215 static time_t t_malloc_fail;
217 #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1)
218 static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
219 "Kernel malloc debugging options");
223 * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
224 * the caller specifies M_NOWAIT. If set to 0, no failures are caused.
226 #ifdef MALLOC_MAKE_FAILURES
227 static int malloc_failure_rate;
228 static int malloc_nowait_count;
229 static int malloc_failure_count;
230 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN,
231 &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
232 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
233 &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
237 sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS)
241 size = vmem_size(kmem_arena, VMEM_ALLOC);
242 return (sysctl_handle_long(oidp, &size, 0, req));
246 sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS)
250 size = vmem_size(kmem_arena, VMEM_FREE);
251 return (sysctl_handle_long(oidp, &size, 0, req));
255 * malloc(9) uma zone separation -- sub-page buffer overruns in one
256 * malloc type will affect only a subset of other malloc types.
258 #if MALLOC_DEBUG_MAXZONES > 1
260 tunable_set_numzones(void)
263 TUNABLE_INT_FETCH("debug.malloc.numzones",
266 /* Sanity check the number of malloc uma zones. */
269 if (numzones > MALLOC_DEBUG_MAXZONES)
270 numzones = MALLOC_DEBUG_MAXZONES;
272 SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL);
273 SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
274 &numzones, 0, "Number of malloc uma subzones");
277 * Any number that changes regularly is an okay choice for the
278 * offset. Build numbers are pretty good of you have them.
280 static u_int zone_offset = __FreeBSD_version;
281 TUNABLE_INT("debug.malloc.zone_offset", &zone_offset);
282 SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN,
283 &zone_offset, 0, "Separate malloc types by examining the "
284 "Nth character in the malloc type short description.");
287 mtp_get_subzone(const char *desc)
292 if (desc == NULL || (len = strlen(desc)) == 0)
294 val = desc[zone_offset % len];
295 return (val % numzones);
297 #elif MALLOC_DEBUG_MAXZONES == 0
298 #error "MALLOC_DEBUG_MAXZONES must be positive."
301 mtp_get_subzone(const char *desc)
306 #endif /* MALLOC_DEBUG_MAXZONES > 1 */
309 malloc_last_fail(void)
312 return (time_uptime - t_malloc_fail);
316 * An allocation has succeeded -- update malloc type statistics for the
317 * amount of bucket size. Occurs within a critical section so that the
318 * thread isn't preempted and doesn't migrate while updating per-PCU
322 malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
325 struct malloc_type_internal *mtip;
326 struct malloc_type_stats *mtsp;
329 mtip = mtp->ks_handle;
330 mtsp = &mtip->mti_stats[curcpu];
332 mtsp->mts_memalloced += size;
333 mtsp->mts_numallocs++;
336 mtsp->mts_size |= 1 << zindx;
339 if (dtrace_malloc_probe != NULL) {
340 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC];
342 (dtrace_malloc_probe)(probe_id,
343 (uintptr_t) mtp, (uintptr_t) mtip,
344 (uintptr_t) mtsp, size, zindx);
352 malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
356 malloc_type_zone_allocated(mtp, size, -1);
360 * A free operation has occurred -- update malloc type statistics for the
361 * amount of the bucket size. Occurs within a critical section so that the
362 * thread isn't preempted and doesn't migrate while updating per-CPU
366 malloc_type_freed(struct malloc_type *mtp, unsigned long size)
368 struct malloc_type_internal *mtip;
369 struct malloc_type_stats *mtsp;
372 mtip = mtp->ks_handle;
373 mtsp = &mtip->mti_stats[curcpu];
374 mtsp->mts_memfreed += size;
375 mtsp->mts_numfrees++;
378 if (dtrace_malloc_probe != NULL) {
379 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE];
381 (dtrace_malloc_probe)(probe_id,
382 (uintptr_t) mtp, (uintptr_t) mtip,
383 (uintptr_t) mtsp, size, 0);
393 * Allocate a block of physically contiguous memory.
395 * If M_NOWAIT is set, this routine will not block and return NULL if
396 * the allocation fails.
399 contigmalloc(unsigned long size, struct malloc_type *type, int flags,
400 vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
405 ret = (void *)kmem_alloc_contig(kernel_arena, size, flags, low, high,
406 alignment, boundary, VM_MEMATTR_DEFAULT);
408 malloc_type_allocated(type, round_page(size));
415 * Free a block of memory allocated by contigmalloc.
417 * This routine may not block.
420 contigfree(void *addr, unsigned long size, struct malloc_type *type)
423 kmem_free(kernel_arena, (vm_offset_t)addr, size);
424 malloc_type_freed(type, round_page(size));
430 * Allocate a block of memory.
432 * If M_NOWAIT is set, this routine will not block and return NULL if
433 * the allocation fails.
436 malloc(unsigned long size, struct malloc_type *mtp, int flags)
439 struct malloc_type_internal *mtip;
442 #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
443 unsigned long osize = size;
447 KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic"));
449 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
451 indx = flags & (M_WAITOK | M_NOWAIT);
452 if (indx != M_NOWAIT && indx != M_WAITOK) {
453 static struct timeval lasterr;
454 static int curerr, once;
455 if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
456 printf("Bad malloc flags: %x\n", indx);
463 #ifdef MALLOC_MAKE_FAILURES
464 if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
465 atomic_add_int(&malloc_nowait_count, 1);
466 if ((malloc_nowait_count % malloc_failure_rate) == 0) {
467 atomic_add_int(&malloc_failure_count, 1);
468 t_malloc_fail = time_uptime;
473 if (flags & M_WAITOK)
474 KASSERT(curthread->td_intr_nesting_level == 0,
475 ("malloc(M_WAITOK) in interrupt context"));
476 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
477 ("malloc: called with spinlock or critical section held"));
479 #ifdef DEBUG_MEMGUARD
480 if (memguard_cmp_mtp(mtp, size)) {
481 va = memguard_alloc(size, flags);
484 /* This is unfortunate but should not be fatal. */
489 size = redzone_size_ntor(size);
492 if (size <= kmem_zmax) {
493 mtip = mtp->ks_handle;
494 if (size & KMEM_ZMASK)
495 size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
496 indx = kmemsize[size >> KMEM_ZSHIFT];
497 KASSERT(mtip->mti_zone < numzones,
498 ("mti_zone %u out of range %d",
499 mtip->mti_zone, numzones));
500 zone = kmemzones[indx].kz_zone[mtip->mti_zone];
501 #ifdef MALLOC_PROFILE
502 krequests[size >> KMEM_ZSHIFT]++;
504 va = uma_zalloc(zone, flags);
506 size = zone->uz_size;
507 malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
509 size = roundup(size, PAGE_SIZE);
511 va = uma_large_malloc(size, flags);
512 malloc_type_allocated(mtp, va == NULL ? 0 : size);
514 if (flags & M_WAITOK)
515 KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
517 t_malloc_fail = time_uptime;
519 if (va != NULL && !(flags & M_ZERO)) {
520 memset(va, 0x70, osize);
525 va = redzone_setup(va, osize);
527 return ((void *) va);
531 mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags)
534 if (WOULD_OVERFLOW(nmemb, size))
535 panic("mallocarray: %zu * %zu overflowed", nmemb, size);
537 return (malloc(size * nmemb, type, flags));
543 * Free a block of memory allocated by malloc.
545 * This routine may not block.
548 free(void *addr, struct malloc_type *mtp)
553 KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic"));
554 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
555 ("free: called with spinlock or critical section held"));
557 /* free(NULL, ...) does nothing */
561 #ifdef DEBUG_MEMGUARD
562 if (is_memguard_addr(addr)) {
570 addr = redzone_addr_ntor(addr);
573 slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
576 panic("free: address %p(%p) has not been allocated.\n",
577 addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
579 if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
581 struct malloc_type **mtpp = addr;
583 size = slab->us_keg->uk_size;
586 * Cache a pointer to the malloc_type that most recently freed
587 * this memory here. This way we know who is most likely to
588 * have stepped on it later.
590 * This code assumes that size is a multiple of 8 bytes for
593 mtpp = (struct malloc_type **)
594 ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
595 mtpp += (size - sizeof(struct malloc_type *)) /
596 sizeof(struct malloc_type *);
599 uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
601 size = slab->us_size;
602 uma_large_free(slab);
604 malloc_type_freed(mtp, size);
608 * realloc: change the size of a memory block
611 realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
617 KASSERT(mtp->ks_magic == M_MAGIC,
618 ("realloc: bad malloc type magic"));
619 KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(),
620 ("realloc: called with spinlock or critical section held"));
622 /* realloc(NULL, ...) is equivalent to malloc(...) */
624 return (malloc(size, mtp, flags));
627 * XXX: Should report free of old memory and alloc of new memory to
631 #ifdef DEBUG_MEMGUARD
632 if (is_memguard_addr(addr))
633 return (memguard_realloc(addr, size, mtp, flags));
638 alloc = redzone_get_size(addr);
640 slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
643 KASSERT(slab != NULL,
644 ("realloc: address %p out of range", (void *)addr));
646 /* Get the size of the original block */
647 if (!(slab->us_flags & UMA_SLAB_MALLOC))
648 alloc = slab->us_keg->uk_size;
650 alloc = slab->us_size;
652 /* Reuse the original block if appropriate */
654 && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
656 #endif /* !DEBUG_REDZONE */
658 /* Allocate a new, bigger (or smaller) block */
659 if ((newaddr = malloc(size, mtp, flags)) == NULL)
662 /* Copy over original contents */
663 bcopy(addr, newaddr, min(size, alloc));
669 * reallocf: same as realloc() but free memory on failure.
672 reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
676 if ((mem = realloc(addr, size, mtp, flags)) == NULL)
682 * Wake the uma reclamation pagedaemon thread when we exhaust KVA. It
683 * will call the lowmem handler and uma_reclaim() callbacks in a
684 * context that is safe.
687 kmem_reclaim(vmem_t *vm, int flags)
690 uma_reclaim_wakeup();
695 CTASSERT(VM_KMEM_SIZE_SCALE >= 1);
699 * Initialize the kernel memory (kmem) arena.
708 if (vm_kmem_size == 0)
709 vm_kmem_size = VM_KMEM_SIZE;
711 #ifdef VM_KMEM_SIZE_MIN
712 if (vm_kmem_size_min == 0)
713 vm_kmem_size_min = VM_KMEM_SIZE_MIN;
715 #ifdef VM_KMEM_SIZE_MAX
716 if (vm_kmem_size_max == 0)
717 vm_kmem_size_max = VM_KMEM_SIZE_MAX;
720 * Calculate the amount of kernel virtual address (KVA) space that is
721 * preallocated to the kmem arena. In order to support a wide range
722 * of machines, it is a function of the physical memory size,
725 * min(max(physical memory size / VM_KMEM_SIZE_SCALE,
726 * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX)
728 * Every architecture must define an integral value for
729 * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN
730 * and VM_KMEM_SIZE_MAX, which represent respectively the floor and
731 * ceiling on this preallocation, are optional. Typically,
732 * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on
733 * a given architecture.
735 mem_size = vm_cnt.v_page_count;
736 if (mem_size <= 32768) /* delphij XXX 128MB */
737 kmem_zmax = PAGE_SIZE;
739 if (vm_kmem_size_scale < 1)
740 vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
743 * Check if we should use defaults for the "vm_kmem_size"
746 if (vm_kmem_size == 0) {
747 vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
749 if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min)
750 vm_kmem_size = vm_kmem_size_min;
751 if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
752 vm_kmem_size = vm_kmem_size_max;
756 * The amount of KVA space that is preallocated to the
757 * kmem arena can be set statically at compile-time or manually
758 * through the kernel environment. However, it is still limited to
759 * twice the physical memory size, which has been sufficient to handle
760 * the most severe cases of external fragmentation in the kmem arena.
762 if (vm_kmem_size / 2 / PAGE_SIZE > mem_size)
763 vm_kmem_size = 2 * mem_size * PAGE_SIZE;
765 vm_kmem_size = round_page(vm_kmem_size);
766 #ifdef DEBUG_MEMGUARD
767 tmp = memguard_fudge(vm_kmem_size, kernel_map);
771 vmem_init(kmem_arena, "kmem arena", kva_alloc(tmp), tmp, PAGE_SIZE,
773 vmem_set_reclaim(kmem_arena, kmem_reclaim);
775 #ifdef DEBUG_MEMGUARD
777 * Initialize MemGuard if support compiled in. MemGuard is a
778 * replacement allocator used for detecting tamper-after-free
779 * scenarios as they occur. It is only used for debugging.
781 memguard_init(kmem_arena);
786 * Initialize the kernel memory allocator
790 mallocinit(void *dummy)
795 mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
801 if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX)
802 kmem_zmax = KMEM_ZMAX;
804 mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal),
806 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
808 NULL, NULL, NULL, NULL,
810 UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
811 for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
812 int size = kmemzones[indx].kz_size;
813 char *name = kmemzones[indx].kz_name;
816 for (subzone = 0; subzone < numzones; subzone++) {
817 kmemzones[indx].kz_zone[subzone] =
818 uma_zcreate(name, size,
820 mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
822 NULL, NULL, NULL, NULL,
824 UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
826 for (;i <= size; i+= KMEM_ZBASE)
827 kmemsize[i >> KMEM_ZSHIFT] = indx;
831 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL);
834 malloc_init(void *data)
836 struct malloc_type_internal *mtip;
837 struct malloc_type *mtp;
839 KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init"));
842 if (mtp->ks_magic != M_MAGIC)
843 panic("malloc_init: bad malloc type magic");
845 mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
846 mtp->ks_handle = mtip;
847 mtip->mti_zone = mtp_get_subzone(mtp->ks_shortdesc);
849 mtx_lock(&malloc_mtx);
850 mtp->ks_next = kmemstatistics;
851 kmemstatistics = mtp;
853 mtx_unlock(&malloc_mtx);
857 malloc_uninit(void *data)
859 struct malloc_type_internal *mtip;
860 struct malloc_type_stats *mtsp;
861 struct malloc_type *mtp, *temp;
863 long temp_allocs, temp_bytes;
867 KASSERT(mtp->ks_magic == M_MAGIC,
868 ("malloc_uninit: bad malloc type magic"));
869 KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
871 mtx_lock(&malloc_mtx);
872 mtip = mtp->ks_handle;
873 mtp->ks_handle = NULL;
874 if (mtp != kmemstatistics) {
875 for (temp = kmemstatistics; temp != NULL;
876 temp = temp->ks_next) {
877 if (temp->ks_next == mtp) {
878 temp->ks_next = mtp->ks_next;
883 ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc));
885 kmemstatistics = mtp->ks_next;
887 mtx_unlock(&malloc_mtx);
890 * Look for memory leaks.
892 temp_allocs = temp_bytes = 0;
893 for (i = 0; i < MAXCPU; i++) {
894 mtsp = &mtip->mti_stats[i];
895 temp_allocs += mtsp->mts_numallocs;
896 temp_allocs -= mtsp->mts_numfrees;
897 temp_bytes += mtsp->mts_memalloced;
898 temp_bytes -= mtsp->mts_memfreed;
900 if (temp_allocs > 0 || temp_bytes > 0) {
901 printf("Warning: memory type %s leaked memory on destroy "
902 "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
903 temp_allocs, temp_bytes);
906 slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK));
907 uma_zfree_arg(mt_zone, mtip, slab);
911 malloc_desc2type(const char *desc)
913 struct malloc_type *mtp;
915 mtx_assert(&malloc_mtx, MA_OWNED);
916 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
917 if (strcmp(mtp->ks_shortdesc, desc) == 0)
924 sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
926 struct malloc_type_stream_header mtsh;
927 struct malloc_type_internal *mtip;
928 struct malloc_type_header mth;
929 struct malloc_type *mtp;
933 error = sysctl_wire_old_buffer(req, 0);
936 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
937 sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL);
938 mtx_lock(&malloc_mtx);
941 * Insert stream header.
943 bzero(&mtsh, sizeof(mtsh));
944 mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
945 mtsh.mtsh_maxcpus = MAXCPU;
946 mtsh.mtsh_count = kmemcount;
947 (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh));
950 * Insert alternating sequence of type headers and type statistics.
952 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
953 mtip = (struct malloc_type_internal *)mtp->ks_handle;
956 * Insert type header.
958 bzero(&mth, sizeof(mth));
959 strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
960 (void)sbuf_bcat(&sbuf, &mth, sizeof(mth));
963 * Insert type statistics for each CPU.
965 for (i = 0; i < MAXCPU; i++) {
966 (void)sbuf_bcat(&sbuf, &mtip->mti_stats[i],
967 sizeof(mtip->mti_stats[i]));
970 mtx_unlock(&malloc_mtx);
971 error = sbuf_finish(&sbuf);
976 SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
977 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats",
978 "Return malloc types");
980 SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
981 "Count of kernel malloc types");
984 malloc_type_list(malloc_type_list_func_t *func, void *arg)
986 struct malloc_type *mtp, **bufmtp;
990 mtx_lock(&malloc_mtx);
992 mtx_assert(&malloc_mtx, MA_OWNED);
994 mtx_unlock(&malloc_mtx);
996 buflen = sizeof(struct malloc_type *) * count;
997 bufmtp = malloc(buflen, M_TEMP, M_WAITOK);
999 mtx_lock(&malloc_mtx);
1001 if (count < kmemcount) {
1002 free(bufmtp, M_TEMP);
1006 for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++)
1009 mtx_unlock(&malloc_mtx);
1011 for (i = 0; i < count; i++)
1012 (func)(bufmtp[i], arg);
1014 free(bufmtp, M_TEMP);
1018 DB_SHOW_COMMAND(malloc, db_show_malloc)
1020 struct malloc_type_internal *mtip;
1021 struct malloc_type *mtp;
1022 uint64_t allocs, frees;
1023 uint64_t alloced, freed;
1026 db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse",
1028 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1029 mtip = (struct malloc_type_internal *)mtp->ks_handle;
1034 for (i = 0; i < MAXCPU; i++) {
1035 allocs += mtip->mti_stats[i].mts_numallocs;
1036 frees += mtip->mti_stats[i].mts_numfrees;
1037 alloced += mtip->mti_stats[i].mts_memalloced;
1038 freed += mtip->mti_stats[i].mts_memfreed;
1040 db_printf("%18s %12ju %12juK %12ju\n",
1041 mtp->ks_shortdesc, allocs - frees,
1042 (alloced - freed + 1023) / 1024, allocs);
1048 #if MALLOC_DEBUG_MAXZONES > 1
1049 DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches)
1051 struct malloc_type_internal *mtip;
1052 struct malloc_type *mtp;
1056 db_printf("Usage: show multizone_matches <malloc type/addr>\n");
1060 if (mtp->ks_magic != M_MAGIC) {
1061 db_printf("Magic %lx does not match expected %x\n",
1062 mtp->ks_magic, M_MAGIC);
1066 mtip = mtp->ks_handle;
1067 subzone = mtip->mti_zone;
1069 for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1070 mtip = mtp->ks_handle;
1071 if (mtip->mti_zone != subzone)
1073 db_printf("%s\n", mtp->ks_shortdesc);
1078 #endif /* MALLOC_DEBUG_MAXZONES > 1 */
1081 #ifdef MALLOC_PROFILE
1084 sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
1098 error = sysctl_wire_old_buffer(req, 0);
1101 sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
1103 "\n Size Requests Real Size\n");
1104 for (i = 0; i < KMEM_ZSIZE; i++) {
1105 size = i << KMEM_ZSHIFT;
1106 rsize = kmemzones[kmemsize[i]].kz_size;
1107 count = (long long unsigned)krequests[i];
1109 sbuf_printf(&sbuf, "%6d%28llu%11d\n", size,
1110 (unsigned long long)count, rsize);
1112 if ((rsize * count) > (size * count))
1113 waste += (rsize * count) - (size * count);
1114 mem += (rsize * count);
1117 "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
1118 (unsigned long long)mem, (unsigned long long)waste);
1119 error = sbuf_finish(&sbuf);
1124 SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
1125 NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
1126 #endif /* MALLOC_PROFILE */