19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1991, 1993 363a7e0a3SRobert Watson * The Regents of the University of California. 463a7e0a3SRobert Watson * Copyright (c) 2005 Robert N. M. Watson 563a7e0a3SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 378a58a9f6SJohn Dyson #include "opt_vm.h" 388a58a9f6SJohn Dyson 39df8bae1dSRodney W. Grimes #include <sys/param.h> 4026f9a767SRodney W. Grimes #include <sys/systm.h> 412d50560aSMarcel Moolenaar #include <sys/kdb.h> 42df8bae1dSRodney W. Grimes #include <sys/kernel.h> 43fb919e4dSMark Murray #include <sys/lock.h> 44df8bae1dSRodney W. Grimes #include <sys/malloc.h> 4554e7152cSDavid Greenman #include <sys/mbuf.h> 46eec258d2SJohn Baldwin #include <sys/mutex.h> 47efeaf95aSDavid Greenman #include <sys/vmmeter.h> 48a448b62aSJake Burkholder #include <sys/proc.h> 4963a7e0a3SRobert Watson #include <sys/sbuf.h> 506f267175SJeff Roberson #include <sys/sysctl.h> 511fb14a47SPoul-Henning Kamp #include <sys/time.h> 529a02e8c6SJason Evans 53df8bae1dSRodney W. Grimes #include <vm/vm.h> 5499571dc3SJeff Roberson #include <vm/pmap.h> 55efeaf95aSDavid Greenman #include <vm/vm_param.h> 56df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 57efeaf95aSDavid Greenman #include <vm/vm_extern.h> 583075778bSJohn Dyson #include <vm/vm_map.h> 5999571dc3SJeff Roberson #include <vm/vm_page.h> 608355f576SJeff Roberson #include <vm/uma.h> 618355f576SJeff Roberson #include <vm/uma_int.h> 628efc4effSJeff Roberson #include <vm/uma_dbg.h> 63df8bae1dSRodney W. Grimes 64e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 65e4eb384bSBosko Milekic #include <vm/memguard.h> 66e4eb384bSBosko Milekic #endif 67e4eb384bSBosko Milekic 68984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 69984982d6SPoul-Henning Kamp #include <machine/cpu.h> 70984982d6SPoul-Henning Kamp #endif 71984982d6SPoul-Henning Kamp 7244a8ff31SArchie Cobbs /* 7344a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 7444a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 7544a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 7644a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 7744a8ff31SArchie Cobbs */ 7844a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 7944a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 8044a8ff31SArchie Cobbs #endif 8144a8ff31SArchie Cobbs 823b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 839ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 849ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 859ef246c6SBruce Evans 8682cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 8782cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 8882cd038dSYoshinobu Inoue 894d77a549SAlfred Perlstein static void kmeminit(void *); 902b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL) 912b14f991SJulian Elischer 92a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list"); 93a1c995b6SPoul-Henning Kamp 94db669378SPeter Wemm static struct malloc_type *kmemstatistics; 95254c6cb3SPoul-Henning Kamp static char *kmembase; 96043a2f3bSBruce Evans static char *kmemlimit; 97cd814b26SRobert Watson static int kmemcount; 981f6889a1SMatthew Dillon 998355f576SJeff Roberson #define KMEM_ZSHIFT 4 1008355f576SJeff Roberson #define KMEM_ZBASE 16 1018355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 1028355f576SJeff Roberson 1039fb535deSJeff Roberson #define KMEM_ZMAX PAGE_SIZE 1048355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 1056f267175SJeff Roberson static u_int8_t kmemsize[KMEM_ZSIZE + 1]; 1066f267175SJeff Roberson 1078355f576SJeff Roberson /* These won't be powers of two for long */ 1088355f576SJeff Roberson struct { 1096f267175SJeff Roberson int kz_size; 1106f267175SJeff Roberson char *kz_name; 1116f267175SJeff Roberson uma_zone_t kz_zone; 1126f267175SJeff Roberson } kmemzones[] = { 1136f267175SJeff Roberson {16, "16", NULL}, 1146f267175SJeff Roberson {32, "32", NULL}, 1156f267175SJeff Roberson {64, "64", NULL}, 1166f267175SJeff Roberson {128, "128", NULL}, 1176f267175SJeff Roberson {256, "256", NULL}, 1186f267175SJeff Roberson {512, "512", NULL}, 1196f267175SJeff Roberson {1024, "1024", NULL}, 1206f267175SJeff Roberson {2048, "2048", NULL}, 1216f267175SJeff Roberson {4096, "4096", NULL}, 1229fb535deSJeff Roberson #if PAGE_SIZE > 4096 1236f267175SJeff Roberson {8192, "8192", NULL}, 1249fb535deSJeff Roberson #if PAGE_SIZE > 8192 12543a7c4e9SRobert Watson {16384, "16384", NULL}, 1269fb535deSJeff Roberson #if PAGE_SIZE > 16384 127bd796eb2SRobert Watson {32768, "32768", NULL}, 1289fb535deSJeff Roberson #if PAGE_SIZE > 32768 129bd796eb2SRobert Watson {65536, "65536", NULL}, 1309fb535deSJeff Roberson #if PAGE_SIZE > 65536 1319fb535deSJeff Roberson #error "Unsupported PAGE_SIZE" 1329fb535deSJeff Roberson #endif /* 65536 */ 1339fb535deSJeff Roberson #endif /* 32768 */ 1349fb535deSJeff Roberson #endif /* 16384 */ 1359fb535deSJeff Roberson #endif /* 8192 */ 1369fb535deSJeff Roberson #endif /* 4096 */ 1378355f576SJeff Roberson {0, NULL}, 1388355f576SJeff Roberson }; 1398355f576SJeff Roberson 14063a7e0a3SRobert Watson static uma_zone_t mt_zone; 14163a7e0a3SRobert Watson 142e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 143e4eb384bSBosko Milekic u_int vm_memguard_divisor; 144e4eb384bSBosko Milekic SYSCTL_UINT(_vm, OID_AUTO, memguard_divisor, CTLFLAG_RD, &vm_memguard_divisor, 145e4eb384bSBosko Milekic 0, "(kmem_size/memguard_divisor) == memguard submap size"); 146e4eb384bSBosko Milekic #endif 147e4eb384bSBosko Milekic 1486f267175SJeff Roberson u_int vm_kmem_size; 14984344f9fSDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0, 15084344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1515a34a9f0SJeff Roberson 152479439b4SDag-Erling Smørgrav u_int vm_kmem_size_max; 153479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0, 154479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 155479439b4SDag-Erling Smørgrav 156479439b4SDag-Erling Smørgrav u_int vm_kmem_size_scale; 157479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0, 158479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 159479439b4SDag-Erling Smørgrav 1605a34a9f0SJeff Roberson /* 16199571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 1625a34a9f0SJeff Roberson */ 1635a34a9f0SJeff Roberson 1645a34a9f0SJeff Roberson struct mtx malloc_mtx; 16569ef67f9SJason Evans 1665e914b96SJeff Roberson #ifdef MALLOC_PROFILE 1675e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 1686f267175SJeff Roberson 1695e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 1705e914b96SJeff Roberson #endif 1715e914b96SJeff Roberson 1725e914b96SJeff Roberson static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS); 173cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 174df8bae1dSRodney W. Grimes 1751fb14a47SPoul-Henning Kamp /* time_uptime of last malloc(9) failure */ 1761fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 1771fb14a47SPoul-Henning Kamp 178eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 179eae870cdSRobert Watson /* 180eae870cdSRobert Watson * Causes malloc failures every (n) mallocs with M_NOWAIT. If set to 0, 181eae870cdSRobert Watson * doesn't cause failures. 182eae870cdSRobert Watson */ 183eae870cdSRobert Watson SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 184eae870cdSRobert Watson "Kernel malloc debugging options"); 185eae870cdSRobert Watson 186eae870cdSRobert Watson static int malloc_failure_rate; 187eae870cdSRobert Watson static int malloc_nowait_count; 188eae870cdSRobert Watson static int malloc_failure_count; 189eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW, 190eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 191f2538508SRobert Watson TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate); 192eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 193eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 194eae870cdSRobert Watson #endif 195eae870cdSRobert Watson 1961fb14a47SPoul-Henning Kamp int 1971fb14a47SPoul-Henning Kamp malloc_last_fail(void) 1981fb14a47SPoul-Henning Kamp { 1991fb14a47SPoul-Henning Kamp 2001fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 2011fb14a47SPoul-Henning Kamp } 2021fb14a47SPoul-Henning Kamp 203df8bae1dSRodney W. Grimes /* 2044362fadaSBrian Feldman * Add this to the informational malloc_type bucket. 2054362fadaSBrian Feldman */ 2064362fadaSBrian Feldman static void 20763a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 2084362fadaSBrian Feldman int zindx) 2094362fadaSBrian Feldman { 21063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 21163a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 21263a7e0a3SRobert Watson 21363a7e0a3SRobert Watson critical_enter(); 21463a7e0a3SRobert Watson mtip = mtp->ks_handle; 21563a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 21663a7e0a3SRobert Watson mtsp->mts_memalloced += size; 21763a7e0a3SRobert Watson mtsp->mts_numallocs++; 2184362fadaSBrian Feldman if (zindx != -1) 21963a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 22063a7e0a3SRobert Watson critical_exit(); 2214362fadaSBrian Feldman } 2224362fadaSBrian Feldman 2234362fadaSBrian Feldman void 22463a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 2254362fadaSBrian Feldman { 22663a7e0a3SRobert Watson 22763a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 2284362fadaSBrian Feldman } 2294362fadaSBrian Feldman 2304362fadaSBrian Feldman /* 2314362fadaSBrian Feldman * Remove this allocation from the informational malloc_type bucket. 2324362fadaSBrian Feldman */ 2334362fadaSBrian Feldman void 23463a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 2354362fadaSBrian Feldman { 23663a7e0a3SRobert Watson struct malloc_type_internal *mtip; 23763a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 23863a7e0a3SRobert Watson 23963a7e0a3SRobert Watson critical_enter(); 24063a7e0a3SRobert Watson mtip = mtp->ks_handle; 24163a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 24263a7e0a3SRobert Watson mtsp->mts_memfreed += size; 24363a7e0a3SRobert Watson mtsp->mts_numfrees++; 24463a7e0a3SRobert Watson critical_exit(); 2454362fadaSBrian Feldman } 2464362fadaSBrian Feldman 2474362fadaSBrian Feldman /* 2481c7c3c6aSMatthew Dillon * malloc: 2491c7c3c6aSMatthew Dillon * 2501c7c3c6aSMatthew Dillon * Allocate a block of memory. 2511c7c3c6aSMatthew Dillon * 2521c7c3c6aSMatthew Dillon * If M_NOWAIT is set, this routine will not block and return NULL if 2531c7c3c6aSMatthew Dillon * the allocation fails. 254df8bae1dSRodney W. Grimes */ 255df8bae1dSRodney W. Grimes void * 25663a7e0a3SRobert Watson malloc(unsigned long size, struct malloc_type *mtp, int flags) 257df8bae1dSRodney W. Grimes { 2586f267175SJeff Roberson int indx; 2598355f576SJeff Roberson caddr_t va; 2608355f576SJeff Roberson uma_zone_t zone; 261099a0e58SBosko Milekic uma_keg_t keg; 2624db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 2634db4f5c8SPoul-Henning Kamp unsigned long osize = size; 2644db4f5c8SPoul-Henning Kamp #endif 265df8bae1dSRodney W. Grimes 266194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 267d3c11994SPoul-Henning Kamp /* 268d3c11994SPoul-Henning Kamp * To make sure that WAITOK or NOWAIT is set, but not more than 269d3c11994SPoul-Henning Kamp * one, and check against the API botches that are common. 270d3c11994SPoul-Henning Kamp */ 271d3c11994SPoul-Henning Kamp indx = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT); 272d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 273d3c11994SPoul-Henning Kamp static struct timeval lasterr; 274d3c11994SPoul-Henning Kamp static int curerr, once; 275d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 276d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 2772d50560aSMarcel Moolenaar kdb_backtrace(); 278d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 279d3c11994SPoul-Henning Kamp once++; 280d3c11994SPoul-Henning Kamp } 281d3c11994SPoul-Henning Kamp } 282194a0abfSPoul-Henning Kamp #endif 283708da94eSPoul-Henning Kamp #if 0 284708da94eSPoul-Henning Kamp if (size == 0) 2852d50560aSMarcel Moolenaar kdb_enter("zero size malloc"); 286708da94eSPoul-Henning Kamp #endif 287eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 288eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 289eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 290eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 291eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 2923f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 293eae870cdSRobert Watson return (NULL); 294eae870cdSRobert Watson } 295eae870cdSRobert Watson } 296eae870cdSRobert Watson #endif 297d3c11994SPoul-Henning Kamp if (flags & M_WAITOK) 298b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 299a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 300e4eb384bSBosko Milekic 301e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 302e4eb384bSBosko Milekic /* XXX CHANGEME! */ 30363a7e0a3SRobert Watson if (mtp == M_SUBPROC) 304e4eb384bSBosko Milekic return memguard_alloc(size, flags); 305e4eb384bSBosko Milekic #endif 306e4eb384bSBosko Milekic 3078355f576SJeff Roberson if (size <= KMEM_ZMAX) { 3086f267175SJeff Roberson if (size & KMEM_ZMASK) 3096f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 3106f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 3116f267175SJeff Roberson zone = kmemzones[indx].kz_zone; 312099a0e58SBosko Milekic keg = zone->uz_keg; 3136f267175SJeff Roberson #ifdef MALLOC_PROFILE 3146f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 3156f267175SJeff Roberson #endif 3168355f576SJeff Roberson va = uma_zalloc(zone, flags); 3174362fadaSBrian Feldman if (va != NULL) 318099a0e58SBosko Milekic size = keg->uk_size; 31963a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 3208355f576SJeff Roberson } else { 3216f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 3228355f576SJeff Roberson zone = NULL; 323099a0e58SBosko Milekic keg = NULL; 3248355f576SJeff Roberson va = uma_large_malloc(size, flags); 32563a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 326df8bae1dSRodney W. Grimes } 3271282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 328a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 3291282e9acSPoul-Henning Kamp else if (va == NULL) 3301fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 3314db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 3321282e9acSPoul-Henning Kamp if (va != NULL && !(flags & M_ZERO)) { 3334db4f5c8SPoul-Henning Kamp memset(va, 0x70, osize); 3344db4f5c8SPoul-Henning Kamp } 3354db4f5c8SPoul-Henning Kamp #endif 336df8bae1dSRodney W. Grimes return ((void *) va); 337df8bae1dSRodney W. Grimes } 338df8bae1dSRodney W. Grimes 339df8bae1dSRodney W. Grimes /* 3401c7c3c6aSMatthew Dillon * free: 3411c7c3c6aSMatthew Dillon * 342df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 3431c7c3c6aSMatthew Dillon * 3441c7c3c6aSMatthew Dillon * This routine may not block. 345df8bae1dSRodney W. Grimes */ 346df8bae1dSRodney W. Grimes void 34763a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 348df8bae1dSRodney W. Grimes { 34999571dc3SJeff Roberson uma_slab_t slab; 35099571dc3SJeff Roberson u_long size; 351254c6cb3SPoul-Henning Kamp 35244a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 35344a8ff31SArchie Cobbs if (addr == NULL) 35444a8ff31SArchie Cobbs return; 35544a8ff31SArchie Cobbs 356e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 357e4eb384bSBosko Milekic /* XXX CHANGEME! */ 35863a7e0a3SRobert Watson if (mtp == M_SUBPROC) { 359e4eb384bSBosko Milekic memguard_free(addr); 360e4eb384bSBosko Milekic return; 361e4eb384bSBosko Milekic } 362e4eb384bSBosko Milekic #endif 363e4eb384bSBosko Milekic 3648355f576SJeff Roberson size = 0; 36569ef67f9SJason Evans 36699571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 3678355f576SJeff Roberson 3688355f576SJeff Roberson if (slab == NULL) 3696f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 37099571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 37199571dc3SJeff Roberson 3728355f576SJeff Roberson 3738355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 3748f70816cSJeff Roberson #ifdef INVARIANTS 37563a7e0a3SRobert Watson struct malloc_type **mtpp = addr; 3768f70816cSJeff Roberson #endif 377099a0e58SBosko Milekic size = slab->us_keg->uk_size; 3788f70816cSJeff Roberson #ifdef INVARIANTS 3798f70816cSJeff Roberson /* 3808f70816cSJeff Roberson * Cache a pointer to the malloc_type that most recently freed 3818f70816cSJeff Roberson * this memory here. This way we know who is most likely to 3828f70816cSJeff Roberson * have stepped on it later. 3838f70816cSJeff Roberson * 3848f70816cSJeff Roberson * This code assumes that size is a multiple of 8 bytes for 3858f70816cSJeff Roberson * 64 bit machines 3868f70816cSJeff Roberson */ 38763a7e0a3SRobert Watson mtpp = (struct malloc_type **) 38863a7e0a3SRobert Watson ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 38963a7e0a3SRobert Watson mtpp += (size - sizeof(struct malloc_type *)) / 3908f70816cSJeff Roberson sizeof(struct malloc_type *); 39163a7e0a3SRobert Watson *mtpp = mtp; 3928f70816cSJeff Roberson #endif 393099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 39414bf02f8SJohn Dyson } else { 3958355f576SJeff Roberson size = slab->us_size; 3968355f576SJeff Roberson uma_large_free(slab); 39714bf02f8SJohn Dyson } 39863a7e0a3SRobert Watson malloc_type_freed(mtp, size); 399df8bae1dSRodney W. Grimes } 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes /* 40244a8ff31SArchie Cobbs * realloc: change the size of a memory block 40344a8ff31SArchie Cobbs */ 40444a8ff31SArchie Cobbs void * 40563a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 40644a8ff31SArchie Cobbs { 4078355f576SJeff Roberson uma_slab_t slab; 40844a8ff31SArchie Cobbs unsigned long alloc; 40944a8ff31SArchie Cobbs void *newaddr; 41044a8ff31SArchie Cobbs 41144a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 41244a8ff31SArchie Cobbs if (addr == NULL) 41363a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 41463a7e0a3SRobert Watson 41563a7e0a3SRobert Watson /* 41663a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 41763a7e0a3SRobert Watson * per-CPU stats. 41863a7e0a3SRobert Watson */ 41944a8ff31SArchie Cobbs 420e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 421e4eb384bSBosko Milekic /* XXX: CHANGEME! */ 42263a7e0a3SRobert Watson if (mtp == M_SUBPROC) { 423e4eb384bSBosko Milekic slab = NULL; 424e4eb384bSBosko Milekic alloc = size; 425e4eb384bSBosko Milekic } else { 426e4eb384bSBosko Milekic #endif 427e4eb384bSBosko Milekic 42899571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 4298355f576SJeff Roberson 43044a8ff31SArchie Cobbs /* Sanity check */ 4318355f576SJeff Roberson KASSERT(slab != NULL, 43244a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 43344a8ff31SArchie Cobbs 43444a8ff31SArchie Cobbs /* Get the size of the original block */ 435099a0e58SBosko Milekic if (slab->us_keg) 436099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 4378355f576SJeff Roberson else 4388355f576SJeff Roberson alloc = slab->us_size; 43944a8ff31SArchie Cobbs 44044a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 44144a8ff31SArchie Cobbs if (size <= alloc 44244a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 44344a8ff31SArchie Cobbs return (addr); 44444a8ff31SArchie Cobbs 445e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 446e4eb384bSBosko Milekic } 447e4eb384bSBosko Milekic #endif 448e4eb384bSBosko Milekic 44944a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 45063a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 45144a8ff31SArchie Cobbs return (NULL); 45244a8ff31SArchie Cobbs 45344a8ff31SArchie Cobbs /* Copy over original contents */ 45444a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 45563a7e0a3SRobert Watson free(addr, mtp); 45644a8ff31SArchie Cobbs return (newaddr); 45744a8ff31SArchie Cobbs } 45844a8ff31SArchie Cobbs 45944a8ff31SArchie Cobbs /* 46044a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 46144a8ff31SArchie Cobbs */ 46244a8ff31SArchie Cobbs void * 46363a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 46444a8ff31SArchie Cobbs { 46544a8ff31SArchie Cobbs void *mem; 46644a8ff31SArchie Cobbs 46763a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 46863a7e0a3SRobert Watson free(addr, mtp); 46944a8ff31SArchie Cobbs return (mem); 47044a8ff31SArchie Cobbs } 47144a8ff31SArchie Cobbs 47244a8ff31SArchie Cobbs /* 473df8bae1dSRodney W. Grimes * Initialize the kernel memory allocator 474df8bae1dSRodney W. Grimes */ 4752b14f991SJulian Elischer /* ARGSUSED*/ 4762b14f991SJulian Elischer static void 47787efd4d5SRobert Watson kmeminit(void *dummy) 478df8bae1dSRodney W. Grimes { 4796f267175SJeff Roberson u_int8_t indx; 48027b8623fSDavid Greenman u_long mem_size; 4818355f576SJeff Roberson int i; 4828a58a9f6SJohn Dyson 4836008862bSJohn Baldwin mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 48469ef67f9SJason Evans 4858a58a9f6SJohn Dyson /* 4868a58a9f6SJohn Dyson * Try to auto-tune the kernel memory size, so that it is 4878a58a9f6SJohn Dyson * more applicable for a wider range of machine sizes. 4888a58a9f6SJohn Dyson * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while 4898a58a9f6SJohn Dyson * a VM_KMEM_SIZE of 12MB is a fair compromise. The 4908a58a9f6SJohn Dyson * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space 4918a58a9f6SJohn Dyson * available, and on an X86 with a total KVA space of 256MB, 4928a58a9f6SJohn Dyson * try to keep VM_KMEM_SIZE_MAX at 80MB or below. 4938a58a9f6SJohn Dyson * 4948a58a9f6SJohn Dyson * Note that the kmem_map is also used by the zone allocator, 4958a58a9f6SJohn Dyson * so make sure that there is enough space. 4968a58a9f6SJohn Dyson */ 497099a0e58SBosko Milekic vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE; 4981795d0cdSPaul Saab mem_size = cnt.v_page_count; 4998a58a9f6SJohn Dyson 5008a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE) 501479439b4SDag-Erling Smørgrav vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 5028a58a9f6SJohn Dyson #endif 503479439b4SDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale); 504479439b4SDag-Erling Smørgrav if (vm_kmem_size_scale > 0 && 505479439b4SDag-Erling Smørgrav (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE)) 506479439b4SDag-Erling Smørgrav vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE; 5078a58a9f6SJohn Dyson 5088a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX) 509479439b4SDag-Erling Smørgrav vm_kmem_size_max = VM_KMEM_SIZE_MAX; 5108a58a9f6SJohn Dyson #endif 511479439b4SDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size_max", &vm_kmem_size_max); 512479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 513479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 5148a58a9f6SJohn Dyson 5158de6e8e1SMike Smith /* Allow final override from the kernel environment */ 51684344f9fSDag-Erling Smørgrav #ifndef BURN_BRIDGES 51784344f9fSDag-Erling Smørgrav if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0) 51884344f9fSDag-Erling Smørgrav printf("kern.vm.kmem.size is now called vm.kmem_size!\n"); 51984344f9fSDag-Erling Smørgrav #endif 52084344f9fSDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size); 5218de6e8e1SMike Smith 52227b8623fSDavid Greenman /* 52327b8623fSDavid Greenman * Limit kmem virtual size to twice the physical memory. 52427b8623fSDavid Greenman * This allows for kmem map sparseness, but limits the size 52527b8623fSDavid Greenman * to something sane. Be careful to not overflow the 32bit 52627b8623fSDavid Greenman * ints while doing the check. 52727b8623fSDavid Greenman */ 5281795d0cdSPaul Saab if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count) 52927b8623fSDavid Greenman vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE; 5308a58a9f6SJohn Dyson 53108442f8aSBosko Milekic /* 532347194c1SMike Silbersack * Tune settings based on the kernel map's size at this time. 533347194c1SMike Silbersack */ 534347194c1SMike Silbersack init_param3(vm_kmem_size / PAGE_SIZE); 535347194c1SMike Silbersack 536df8bae1dSRodney W. Grimes kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, 537099a0e58SBosko Milekic (vm_offset_t *)&kmemlimit, vm_kmem_size); 5383075778bSJohn Dyson kmem_map->system_map = 1; 5398355f576SJeff Roberson 540e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 541e4eb384bSBosko Milekic /* 542e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 543e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 544e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 545e4eb384bSBosko Milekic */ 546e4eb384bSBosko Milekic vm_memguard_divisor = 10; 547e4eb384bSBosko Milekic TUNABLE_INT_FETCH("vm.memguard_divisor", &vm_memguard_divisor); 548e4eb384bSBosko Milekic 549e4eb384bSBosko Milekic /* Pick a conservative value if provided value sucks. */ 550e4eb384bSBosko Milekic if ((vm_memguard_divisor <= 0) || 551e4eb384bSBosko Milekic ((vm_kmem_size / vm_memguard_divisor) == 0)) 552e4eb384bSBosko Milekic vm_memguard_divisor = 10; 553e4eb384bSBosko Milekic memguard_init(kmem_map, vm_kmem_size / vm_memguard_divisor); 554e4eb384bSBosko Milekic #endif 555e4eb384bSBosko Milekic 55699571dc3SJeff Roberson uma_startup2(); 5578355f576SJeff Roberson 55863a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 55963a7e0a3SRobert Watson #ifdef INVARIANTS 56063a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 56163a7e0a3SRobert Watson #else 56263a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 56363a7e0a3SRobert Watson #endif 56463a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 5656f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 5666f267175SJeff Roberson int size = kmemzones[indx].kz_size; 5676f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 5688355f576SJeff Roberson 5698efc4effSJeff Roberson kmemzones[indx].kz_zone = uma_zcreate(name, size, 5708efc4effSJeff Roberson #ifdef INVARIANTS 5718f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 5728efc4effSJeff Roberson #else 5738efc4effSJeff Roberson NULL, NULL, NULL, NULL, 5748efc4effSJeff Roberson #endif 5758efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 5766f267175SJeff Roberson 5778355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 5786f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 5798355f576SJeff Roberson 580df8bae1dSRodney W. Grimes } 581254c6cb3SPoul-Henning Kamp } 582254c6cb3SPoul-Henning Kamp 583db669378SPeter Wemm void 58487efd4d5SRobert Watson malloc_init(void *data) 585254c6cb3SPoul-Henning Kamp { 58663a7e0a3SRobert Watson struct malloc_type_internal *mtip; 58763a7e0a3SRobert Watson struct malloc_type *mtp; 58863a7e0a3SRobert Watson 58963a7e0a3SRobert Watson KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init")); 59063a7e0a3SRobert Watson 59163a7e0a3SRobert Watson mtp = data; 59263a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 59363a7e0a3SRobert Watson mtp->ks_handle = mtip; 594254c6cb3SPoul-Henning Kamp 5956f267175SJeff Roberson mtx_lock(&malloc_mtx); 59663a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 59763a7e0a3SRobert Watson kmemstatistics = mtp; 598cd814b26SRobert Watson kmemcount++; 5996f267175SJeff Roberson mtx_unlock(&malloc_mtx); 600df8bae1dSRodney W. Grimes } 601db669378SPeter Wemm 602db669378SPeter Wemm void 60387efd4d5SRobert Watson malloc_uninit(void *data) 604db669378SPeter Wemm { 60563a7e0a3SRobert Watson struct malloc_type_internal *mtip; 60663a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 607db669378SPeter Wemm 60863a7e0a3SRobert Watson mtp = data; 60963a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 6106f267175SJeff Roberson mtx_lock(&malloc_mtx); 61163a7e0a3SRobert Watson mtip = mtp->ks_handle; 61263a7e0a3SRobert Watson mtp->ks_handle = NULL; 61363a7e0a3SRobert Watson if (mtp != kmemstatistics) { 61463a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 61563a7e0a3SRobert Watson temp = temp->ks_next) { 61663a7e0a3SRobert Watson if (temp->ks_next == mtp) 61763a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 618db669378SPeter Wemm } 61963a7e0a3SRobert Watson } else 62063a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 621cd814b26SRobert Watson kmemcount--; 6226f267175SJeff Roberson mtx_unlock(&malloc_mtx); 6238c61b219SJoseph Koshy uma_zfree(mt_zone, mtip); 624db669378SPeter Wemm } 6256f267175SJeff Roberson 6266f267175SJeff Roberson static int 6276f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS) 6286f267175SJeff Roberson { 62963a7e0a3SRobert Watson struct malloc_type_stats mts_local, *mtsp; 63063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 63163a7e0a3SRobert Watson struct malloc_type *mtp; 63263a7e0a3SRobert Watson struct sbuf sbuf; 63363a7e0a3SRobert Watson long temp_allocs, temp_bytes; 6346f267175SJeff Roberson int linesize = 128; 6356f267175SJeff Roberson int bufsize; 6366f267175SJeff Roberson int first; 6376f267175SJeff Roberson int error; 6386f267175SJeff Roberson char *buf; 6396f267175SJeff Roberson int cnt; 6406f267175SJeff Roberson int i; 6416f267175SJeff Roberson 6426f267175SJeff Roberson cnt = 0; 6436f267175SJeff Roberson 64463a7e0a3SRobert Watson /* Guess at how much room is needed. */ 6456f267175SJeff Roberson mtx_lock(&malloc_mtx); 646cd814b26SRobert Watson cnt = kmemcount; 6475a34a9f0SJeff Roberson mtx_unlock(&malloc_mtx); 64863a7e0a3SRobert Watson 6496f267175SJeff Roberson bufsize = linesize * (cnt + 1); 65063a7e0a3SRobert Watson buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 65163a7e0a3SRobert Watson sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN); 65263a7e0a3SRobert Watson 6535a34a9f0SJeff Roberson mtx_lock(&malloc_mtx); 65463a7e0a3SRobert Watson sbuf_printf(&sbuf, 6556f267175SJeff Roberson "\n Type InUse MemUse HighUse Requests Size(s)\n"); 65663a7e0a3SRobert Watson for (mtp = kmemstatistics; cnt != 0 && mtp != NULL; 65763a7e0a3SRobert Watson mtp = mtp->ks_next, cnt--) { 65863a7e0a3SRobert Watson mtip = mtp->ks_handle; 65963a7e0a3SRobert Watson bzero(&mts_local, sizeof(mts_local)); 66063a7e0a3SRobert Watson for (i = 0; i < MAXCPU; i++) { 66163a7e0a3SRobert Watson mtsp = &mtip->mti_stats[i]; 66263a7e0a3SRobert Watson mts_local.mts_memalloced += mtsp->mts_memalloced; 66363a7e0a3SRobert Watson mts_local.mts_memfreed += mtsp->mts_memfreed; 66463a7e0a3SRobert Watson mts_local.mts_numallocs += mtsp->mts_numallocs; 66563a7e0a3SRobert Watson mts_local.mts_numfrees += mtsp->mts_numfrees; 66663a7e0a3SRobert Watson mts_local.mts_size |= mtsp->mts_size; 66763a7e0a3SRobert Watson } 66863a7e0a3SRobert Watson if (mts_local.mts_numallocs == 0) 6696f267175SJeff Roberson continue; 6706f267175SJeff Roberson 67163a7e0a3SRobert Watson /* 67263a7e0a3SRobert Watson * Due to races in per-CPU statistics gather, it's possible to 67363a7e0a3SRobert Watson * get a slightly negative number here. If we do, approximate 67463a7e0a3SRobert Watson * with 0. 67563a7e0a3SRobert Watson */ 67663a7e0a3SRobert Watson if (mts_local.mts_numallocs > mts_local.mts_numfrees) 67763a7e0a3SRobert Watson temp_allocs = mts_local.mts_numallocs - 67863a7e0a3SRobert Watson mts_local.mts_numfrees; 67963a7e0a3SRobert Watson else 68063a7e0a3SRobert Watson temp_allocs = 0; 68163a7e0a3SRobert Watson 68263a7e0a3SRobert Watson /* 68363a7e0a3SRobert Watson * Ditto for bytes allocated. 68463a7e0a3SRobert Watson */ 68563a7e0a3SRobert Watson if (mts_local.mts_memalloced > mts_local.mts_memfreed) 68663a7e0a3SRobert Watson temp_bytes = mts_local.mts_memalloced - 68763a7e0a3SRobert Watson mts_local.mts_memfreed; 68863a7e0a3SRobert Watson else 68963a7e0a3SRobert Watson temp_bytes = 0; 69063a7e0a3SRobert Watson 69163a7e0a3SRobert Watson /* 692cd814b26SRobert Watson * High-waterwark is no longer easily available, so we just 693cd814b26SRobert Watson * print '-' for that column. 69463a7e0a3SRobert Watson */ 695cd814b26SRobert Watson sbuf_printf(&sbuf, "%13s%6lu%6luK -%9llu", 69663a7e0a3SRobert Watson mtp->ks_shortdesc, 69763a7e0a3SRobert Watson temp_allocs, 69863a7e0a3SRobert Watson (temp_bytes + 1023) / 1024, 69963a7e0a3SRobert Watson mts_local.mts_numallocs); 7006f267175SJeff Roberson 7016f267175SJeff Roberson first = 1; 702280759e7SRobert Drehmel for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1; 703280759e7SRobert Drehmel i++) { 70463a7e0a3SRobert Watson if (mts_local.mts_size & (1 << i)) { 7056f267175SJeff Roberson if (first) 70663a7e0a3SRobert Watson sbuf_printf(&sbuf, " "); 7076f267175SJeff Roberson else 70863a7e0a3SRobert Watson sbuf_printf(&sbuf, ","); 70963a7e0a3SRobert Watson sbuf_printf(&sbuf, "%s", 71063a7e0a3SRobert Watson kmemzones[i].kz_name); 7116f267175SJeff Roberson first = 0; 7126f267175SJeff Roberson } 713280759e7SRobert Drehmel } 71463a7e0a3SRobert Watson sbuf_printf(&sbuf, "\n"); 7156f267175SJeff Roberson } 71663a7e0a3SRobert Watson sbuf_finish(&sbuf); 7176f267175SJeff Roberson mtx_unlock(&malloc_mtx); 7186f267175SJeff Roberson 71963a7e0a3SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 72063a7e0a3SRobert Watson 72163a7e0a3SRobert Watson sbuf_delete(&sbuf); 7226f267175SJeff Roberson free(buf, M_TEMP); 7236f267175SJeff Roberson return (error); 7246f267175SJeff Roberson } 7256f267175SJeff Roberson 7266f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD, 7276f267175SJeff Roberson NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats"); 7285e914b96SJeff Roberson 729cd814b26SRobert Watson static int 730cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 731cd814b26SRobert Watson { 732cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 733cd814b26SRobert Watson struct malloc_type_internal *mtip; 734cd814b26SRobert Watson struct malloc_type_header mth; 735cd814b26SRobert Watson struct malloc_type *mtp; 736cd814b26SRobert Watson int buflen, count, error, i; 737cd814b26SRobert Watson struct sbuf sbuf; 738cd814b26SRobert Watson char *buffer; 739cd814b26SRobert Watson 740cd814b26SRobert Watson mtx_lock(&malloc_mtx); 741cd814b26SRobert Watson restart: 742cd814b26SRobert Watson mtx_assert(&malloc_mtx, MA_OWNED); 743cd814b26SRobert Watson count = kmemcount; 744cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 745cd814b26SRobert Watson buflen = sizeof(mtsh) + count * (sizeof(mth) + 746cd814b26SRobert Watson sizeof(struct malloc_type_stats) * MAXCPU) + 1; 747cd814b26SRobert Watson buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 748cd814b26SRobert Watson mtx_lock(&malloc_mtx); 749cd814b26SRobert Watson if (count < kmemcount) { 750cd814b26SRobert Watson free(buffer, M_TEMP); 751cd814b26SRobert Watson goto restart; 752cd814b26SRobert Watson } 753cd814b26SRobert Watson 754cd814b26SRobert Watson sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN); 755cd814b26SRobert Watson 756cd814b26SRobert Watson /* 757cd814b26SRobert Watson * Insert stream header. 758cd814b26SRobert Watson */ 759cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 760cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 761cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 762cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 763cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) { 764cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 765cd814b26SRobert Watson error = ENOMEM; 766cd814b26SRobert Watson goto out; 767cd814b26SRobert Watson } 768cd814b26SRobert Watson 769cd814b26SRobert Watson /* 770cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 771cd814b26SRobert Watson */ 772cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 773cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 774cd814b26SRobert Watson 775cd814b26SRobert Watson /* 776cd814b26SRobert Watson * Insert type header. 777cd814b26SRobert Watson */ 778cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 779cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 780cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) { 781cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 782cd814b26SRobert Watson error = ENOMEM; 783cd814b26SRobert Watson goto out; 784cd814b26SRobert Watson } 785cd814b26SRobert Watson 786cd814b26SRobert Watson /* 787cd814b26SRobert Watson * Insert type statistics for each CPU. 788cd814b26SRobert Watson */ 789cd814b26SRobert Watson for (i = 0; i < MAXCPU; i++) { 790cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mtip->mti_stats[i], 791cd814b26SRobert Watson sizeof(mtip->mti_stats[i])) < 0) { 792cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 793cd814b26SRobert Watson error = ENOMEM; 794cd814b26SRobert Watson goto out; 795cd814b26SRobert Watson } 796cd814b26SRobert Watson } 797cd814b26SRobert Watson } 798cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 799cd814b26SRobert Watson sbuf_finish(&sbuf); 800cd814b26SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 801cd814b26SRobert Watson out: 802cd814b26SRobert Watson sbuf_delete(&sbuf); 803cd814b26SRobert Watson free(buffer, M_TEMP); 804cd814b26SRobert Watson return (error); 805cd814b26SRobert Watson } 806cd814b26SRobert Watson 807cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 808cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 809cd814b26SRobert Watson "Return malloc types"); 810cd814b26SRobert Watson 811cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 812cd814b26SRobert Watson "Count of kernel malloc types"); 813cd814b26SRobert Watson 8145e914b96SJeff Roberson #ifdef MALLOC_PROFILE 8155e914b96SJeff Roberson 8165e914b96SJeff Roberson static int 8175e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 8185e914b96SJeff Roberson { 8195e914b96SJeff Roberson int linesize = 64; 82063a7e0a3SRobert Watson struct sbuf sbuf; 8215e914b96SJeff Roberson uint64_t count; 8225e914b96SJeff Roberson uint64_t waste; 8235e914b96SJeff Roberson uint64_t mem; 8245e914b96SJeff Roberson int bufsize; 8255e914b96SJeff Roberson int error; 8265e914b96SJeff Roberson char *buf; 8275e914b96SJeff Roberson int rsize; 8285e914b96SJeff Roberson int size; 8295e914b96SJeff Roberson int i; 8305e914b96SJeff Roberson 8315e914b96SJeff Roberson bufsize = linesize * (KMEM_ZSIZE + 1); 8325e914b96SJeff Roberson bufsize += 128; /* For the stats line */ 8335e914b96SJeff Roberson bufsize += 128; /* For the banner line */ 8345e914b96SJeff Roberson waste = 0; 8355e914b96SJeff Roberson mem = 0; 8365e914b96SJeff Roberson 83763a7e0a3SRobert Watson buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 83863a7e0a3SRobert Watson sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN); 83963a7e0a3SRobert Watson sbuf_printf(&sbuf, 8405e914b96SJeff Roberson "\n Size Requests Real Size\n"); 8415e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 8425e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 8435e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 8445e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 8455e914b96SJeff Roberson 84663a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 84763a7e0a3SRobert Watson (unsigned long long)count, rsize); 8485e914b96SJeff Roberson 8495e914b96SJeff Roberson if ((rsize * count) > (size * count)) 8505e914b96SJeff Roberson waste += (rsize * count) - (size * count); 8515e914b96SJeff Roberson mem += (rsize * count); 8525e914b96SJeff Roberson } 85363a7e0a3SRobert Watson sbuf_printf(&sbuf, 8545e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 8555e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 85663a7e0a3SRobert Watson sbuf_finish(&sbuf); 8575e914b96SJeff Roberson 85863a7e0a3SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 8595e914b96SJeff Roberson 86063a7e0a3SRobert Watson sbuf_delete(&sbuf); 8615e914b96SJeff Roberson free(buf, M_TEMP); 8625e914b96SJeff Roberson return (error); 8635e914b96SJeff Roberson } 8645e914b96SJeff Roberson 8655e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 8665e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 8675e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 868