1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 30df8bae1dSRodney W. Grimes */ 31df8bae1dSRodney W. Grimes 32677b542eSDavid E. O'Brien #include <sys/cdefs.h> 33677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 34677b542eSDavid E. O'Brien 358a58a9f6SJohn Dyson #include "opt_vm.h" 368a58a9f6SJohn Dyson 37df8bae1dSRodney W. Grimes #include <sys/param.h> 3826f9a767SRodney W. Grimes #include <sys/systm.h> 392d50560aSMarcel Moolenaar #include <sys/kdb.h> 40df8bae1dSRodney W. Grimes #include <sys/kernel.h> 41fb919e4dSMark Murray #include <sys/lock.h> 42df8bae1dSRodney W. Grimes #include <sys/malloc.h> 4354e7152cSDavid Greenman #include <sys/mbuf.h> 44eec258d2SJohn Baldwin #include <sys/mutex.h> 45efeaf95aSDavid Greenman #include <sys/vmmeter.h> 46a448b62aSJake Burkholder #include <sys/proc.h> 476f267175SJeff Roberson #include <sys/sysctl.h> 481fb14a47SPoul-Henning Kamp #include <sys/time.h> 499a02e8c6SJason Evans 50df8bae1dSRodney W. Grimes #include <vm/vm.h> 5199571dc3SJeff Roberson #include <vm/pmap.h> 52efeaf95aSDavid Greenman #include <vm/vm_param.h> 53df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 54efeaf95aSDavid Greenman #include <vm/vm_extern.h> 553075778bSJohn Dyson #include <vm/vm_map.h> 5699571dc3SJeff Roberson #include <vm/vm_page.h> 578355f576SJeff Roberson #include <vm/uma.h> 588355f576SJeff Roberson #include <vm/uma_int.h> 598efc4effSJeff Roberson #include <vm/uma_dbg.h> 60df8bae1dSRodney W. Grimes 61984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 62984982d6SPoul-Henning Kamp #include <machine/cpu.h> 63984982d6SPoul-Henning Kamp #endif 64984982d6SPoul-Henning Kamp 6544a8ff31SArchie Cobbs /* 6644a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 6744a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 6844a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 6944a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 7044a8ff31SArchie Cobbs */ 7144a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 7244a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 7344a8ff31SArchie Cobbs #endif 7444a8ff31SArchie Cobbs 753b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 769ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 779ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 789ef246c6SBruce Evans 7982cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 8082cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 8182cd038dSYoshinobu Inoue 824d77a549SAlfred Perlstein static void kmeminit(void *); 832b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL) 842b14f991SJulian Elischer 85a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list"); 86a1c995b6SPoul-Henning Kamp 87db669378SPeter Wemm static struct malloc_type *kmemstatistics; 88254c6cb3SPoul-Henning Kamp static char *kmembase; 89043a2f3bSBruce Evans static char *kmemlimit; 901f6889a1SMatthew Dillon 918355f576SJeff Roberson #define KMEM_ZSHIFT 4 928355f576SJeff Roberson #define KMEM_ZBASE 16 938355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 948355f576SJeff Roberson 959fb535deSJeff Roberson #define KMEM_ZMAX PAGE_SIZE 968355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 976f267175SJeff Roberson static u_int8_t kmemsize[KMEM_ZSIZE + 1]; 986f267175SJeff Roberson 998355f576SJeff Roberson /* These won't be powers of two for long */ 1008355f576SJeff Roberson struct { 1016f267175SJeff Roberson int kz_size; 1026f267175SJeff Roberson char *kz_name; 1036f267175SJeff Roberson uma_zone_t kz_zone; 1046f267175SJeff Roberson } kmemzones[] = { 1056f267175SJeff Roberson {16, "16", NULL}, 1066f267175SJeff Roberson {32, "32", NULL}, 1076f267175SJeff Roberson {64, "64", NULL}, 1086f267175SJeff Roberson {128, "128", NULL}, 1096f267175SJeff Roberson {256, "256", NULL}, 1106f267175SJeff Roberson {512, "512", NULL}, 1116f267175SJeff Roberson {1024, "1024", NULL}, 1126f267175SJeff Roberson {2048, "2048", NULL}, 1136f267175SJeff Roberson {4096, "4096", NULL}, 1149fb535deSJeff Roberson #if PAGE_SIZE > 4096 1156f267175SJeff Roberson {8192, "8192", NULL}, 1169fb535deSJeff Roberson #if PAGE_SIZE > 8192 11743a7c4e9SRobert Watson {16384, "16384", NULL}, 1189fb535deSJeff Roberson #if PAGE_SIZE > 16384 119bd796eb2SRobert Watson {32768, "32768", NULL}, 1209fb535deSJeff Roberson #if PAGE_SIZE > 32768 121bd796eb2SRobert Watson {65536, "65536", NULL}, 1229fb535deSJeff Roberson #if PAGE_SIZE > 65536 1239fb535deSJeff Roberson #error "Unsupported PAGE_SIZE" 1249fb535deSJeff Roberson #endif /* 65536 */ 1259fb535deSJeff Roberson #endif /* 32768 */ 1269fb535deSJeff Roberson #endif /* 16384 */ 1279fb535deSJeff Roberson #endif /* 8192 */ 1289fb535deSJeff Roberson #endif /* 4096 */ 1298355f576SJeff Roberson {0, NULL}, 1308355f576SJeff Roberson }; 1318355f576SJeff Roberson 1326f267175SJeff Roberson u_int vm_kmem_size; 13384344f9fSDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0, 13484344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1355a34a9f0SJeff Roberson 1365a34a9f0SJeff Roberson /* 13799571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 1385a34a9f0SJeff Roberson */ 1395a34a9f0SJeff Roberson 1405a34a9f0SJeff Roberson struct mtx malloc_mtx; 14169ef67f9SJason Evans 1425e914b96SJeff Roberson #ifdef MALLOC_PROFILE 1435e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 1446f267175SJeff Roberson 1455e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 1465e914b96SJeff Roberson #endif 1475e914b96SJeff Roberson 1485e914b96SJeff Roberson static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS); 149df8bae1dSRodney W. Grimes 1501fb14a47SPoul-Henning Kamp /* time_uptime of last malloc(9) failure */ 1511fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 1521fb14a47SPoul-Henning Kamp 153eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 154eae870cdSRobert Watson /* 155eae870cdSRobert Watson * Causes malloc failures every (n) mallocs with M_NOWAIT. If set to 0, 156eae870cdSRobert Watson * doesn't cause failures. 157eae870cdSRobert Watson */ 158eae870cdSRobert Watson SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 159eae870cdSRobert Watson "Kernel malloc debugging options"); 160eae870cdSRobert Watson 161eae870cdSRobert Watson static int malloc_failure_rate; 162eae870cdSRobert Watson static int malloc_nowait_count; 163eae870cdSRobert Watson static int malloc_failure_count; 164eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW, 165eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 166f2538508SRobert Watson TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate); 167eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 168eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 169eae870cdSRobert Watson #endif 170eae870cdSRobert Watson 1711fb14a47SPoul-Henning Kamp int 1721fb14a47SPoul-Henning Kamp malloc_last_fail(void) 1731fb14a47SPoul-Henning Kamp { 1741fb14a47SPoul-Henning Kamp 1751fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 1761fb14a47SPoul-Henning Kamp } 1771fb14a47SPoul-Henning Kamp 178df8bae1dSRodney W. Grimes /* 1794362fadaSBrian Feldman * Add this to the informational malloc_type bucket. 1804362fadaSBrian Feldman */ 1814362fadaSBrian Feldman static void 1824362fadaSBrian Feldman malloc_type_zone_allocated(struct malloc_type *ksp, unsigned long size, 1834362fadaSBrian Feldman int zindx) 1844362fadaSBrian Feldman { 1854362fadaSBrian Feldman mtx_lock(&ksp->ks_mtx); 1864362fadaSBrian Feldman ksp->ks_calls++; 1874362fadaSBrian Feldman if (zindx != -1) 1884362fadaSBrian Feldman ksp->ks_size |= 1 << zindx; 1894362fadaSBrian Feldman if (size != 0) { 1904362fadaSBrian Feldman ksp->ks_memuse += size; 1914362fadaSBrian Feldman ksp->ks_inuse++; 1924362fadaSBrian Feldman if (ksp->ks_memuse > ksp->ks_maxused) 1934362fadaSBrian Feldman ksp->ks_maxused = ksp->ks_memuse; 1944362fadaSBrian Feldman } 1954362fadaSBrian Feldman mtx_unlock(&ksp->ks_mtx); 1964362fadaSBrian Feldman } 1974362fadaSBrian Feldman 1984362fadaSBrian Feldman void 1994362fadaSBrian Feldman malloc_type_allocated(struct malloc_type *ksp, unsigned long size) 2004362fadaSBrian Feldman { 2014362fadaSBrian Feldman malloc_type_zone_allocated(ksp, size, -1); 2024362fadaSBrian Feldman } 2034362fadaSBrian Feldman 2044362fadaSBrian Feldman /* 2054362fadaSBrian Feldman * Remove this allocation from the informational malloc_type bucket. 2064362fadaSBrian Feldman */ 2074362fadaSBrian Feldman void 2084362fadaSBrian Feldman malloc_type_freed(struct malloc_type *ksp, unsigned long size) 2094362fadaSBrian Feldman { 2104362fadaSBrian Feldman mtx_lock(&ksp->ks_mtx); 2114362fadaSBrian Feldman KASSERT(size <= ksp->ks_memuse, 2124362fadaSBrian Feldman ("malloc(9)/free(9) confusion.\n%s", 2134362fadaSBrian Feldman "Probably freeing with wrong type, but maybe not here.")); 2144362fadaSBrian Feldman ksp->ks_memuse -= size; 2154362fadaSBrian Feldman ksp->ks_inuse--; 2164362fadaSBrian Feldman mtx_unlock(&ksp->ks_mtx); 2174362fadaSBrian Feldman } 2184362fadaSBrian Feldman 2194362fadaSBrian Feldman /* 2201c7c3c6aSMatthew Dillon * malloc: 2211c7c3c6aSMatthew Dillon * 2221c7c3c6aSMatthew Dillon * Allocate a block of memory. 2231c7c3c6aSMatthew Dillon * 2241c7c3c6aSMatthew Dillon * If M_NOWAIT is set, this routine will not block and return NULL if 2251c7c3c6aSMatthew Dillon * the allocation fails. 226df8bae1dSRodney W. Grimes */ 227df8bae1dSRodney W. Grimes void * 228df8bae1dSRodney W. Grimes malloc(size, type, flags) 229df8bae1dSRodney W. Grimes unsigned long size; 23060a513e9SPoul-Henning Kamp struct malloc_type *type; 231254c6cb3SPoul-Henning Kamp int flags; 232df8bae1dSRodney W. Grimes { 2336f267175SJeff Roberson int indx; 2348355f576SJeff Roberson caddr_t va; 2358355f576SJeff Roberson uma_zone_t zone; 236099a0e58SBosko Milekic uma_keg_t keg; 2374db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 2384db4f5c8SPoul-Henning Kamp unsigned long osize = size; 2394db4f5c8SPoul-Henning Kamp #endif 240df8bae1dSRodney W. Grimes 241194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 242d3c11994SPoul-Henning Kamp /* 243d3c11994SPoul-Henning Kamp * To make sure that WAITOK or NOWAIT is set, but not more than 244d3c11994SPoul-Henning Kamp * one, and check against the API botches that are common. 245d3c11994SPoul-Henning Kamp */ 246d3c11994SPoul-Henning Kamp indx = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT); 247d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 248d3c11994SPoul-Henning Kamp static struct timeval lasterr; 249d3c11994SPoul-Henning Kamp static int curerr, once; 250d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 251d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 2522d50560aSMarcel Moolenaar kdb_backtrace(); 253d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 254d3c11994SPoul-Henning Kamp once++; 255d3c11994SPoul-Henning Kamp } 256d3c11994SPoul-Henning Kamp } 257194a0abfSPoul-Henning Kamp #endif 258708da94eSPoul-Henning Kamp #if 0 259708da94eSPoul-Henning Kamp if (size == 0) 2602d50560aSMarcel Moolenaar kdb_enter("zero size malloc"); 261708da94eSPoul-Henning Kamp #endif 262eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 263eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 264eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 265eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 266eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 2673f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 268eae870cdSRobert Watson return (NULL); 269eae870cdSRobert Watson } 270eae870cdSRobert Watson } 271eae870cdSRobert Watson #endif 272d3c11994SPoul-Henning Kamp if (flags & M_WAITOK) 273b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 274a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 2758355f576SJeff Roberson if (size <= KMEM_ZMAX) { 2766f267175SJeff Roberson if (size & KMEM_ZMASK) 2776f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 2786f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 2796f267175SJeff Roberson zone = kmemzones[indx].kz_zone; 280099a0e58SBosko Milekic keg = zone->uz_keg; 2816f267175SJeff Roberson #ifdef MALLOC_PROFILE 2826f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 2836f267175SJeff Roberson #endif 2848355f576SJeff Roberson va = uma_zalloc(zone, flags); 2854362fadaSBrian Feldman if (va != NULL) 286099a0e58SBosko Milekic size = keg->uk_size; 2874362fadaSBrian Feldman malloc_type_zone_allocated(type, va == NULL ? 0 : size, indx); 2888355f576SJeff Roberson } else { 2896f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 2908355f576SJeff Roberson zone = NULL; 291099a0e58SBosko Milekic keg = NULL; 2928355f576SJeff Roberson va = uma_large_malloc(size, flags); 2934362fadaSBrian Feldman malloc_type_allocated(type, va == NULL ? 0 : size); 294df8bae1dSRodney W. Grimes } 2951282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 296a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 2971282e9acSPoul-Henning Kamp else if (va == NULL) 2981fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 2994db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 3001282e9acSPoul-Henning Kamp if (va != NULL && !(flags & M_ZERO)) { 3014db4f5c8SPoul-Henning Kamp memset(va, 0x70, osize); 3024db4f5c8SPoul-Henning Kamp } 3034db4f5c8SPoul-Henning Kamp #endif 304df8bae1dSRodney W. Grimes return ((void *) va); 305df8bae1dSRodney W. Grimes } 306df8bae1dSRodney W. Grimes 307df8bae1dSRodney W. Grimes /* 3081c7c3c6aSMatthew Dillon * free: 3091c7c3c6aSMatthew Dillon * 310df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 3111c7c3c6aSMatthew Dillon * 3121c7c3c6aSMatthew Dillon * This routine may not block. 313df8bae1dSRodney W. Grimes */ 314df8bae1dSRodney W. Grimes void 31568f2d20bSPoul-Henning Kamp free(addr, type) 31668f2d20bSPoul-Henning Kamp void *addr; 31760a513e9SPoul-Henning Kamp struct malloc_type *type; 318df8bae1dSRodney W. Grimes { 31999571dc3SJeff Roberson uma_slab_t slab; 32099571dc3SJeff Roberson u_long size; 321254c6cb3SPoul-Henning Kamp 32244a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 32344a8ff31SArchie Cobbs if (addr == NULL) 32444a8ff31SArchie Cobbs return; 32544a8ff31SArchie Cobbs 3264362fadaSBrian Feldman KASSERT(type->ks_memuse > 0, 3278cb72d61SPoul-Henning Kamp ("malloc(9)/free(9) confusion.\n%s", 3288cb72d61SPoul-Henning Kamp "Probably freeing with wrong type, but maybe not here.")); 3298355f576SJeff Roberson size = 0; 33069ef67f9SJason Evans 33199571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 3328355f576SJeff Roberson 3338355f576SJeff Roberson if (slab == NULL) 3346f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 33599571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 33699571dc3SJeff Roberson 3378355f576SJeff Roberson 3388355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 3398f70816cSJeff Roberson #ifdef INVARIANTS 3408f70816cSJeff Roberson struct malloc_type **mtp = addr; 3418f70816cSJeff Roberson #endif 342099a0e58SBosko Milekic size = slab->us_keg->uk_size; 3438f70816cSJeff Roberson #ifdef INVARIANTS 3448f70816cSJeff Roberson /* 3458f70816cSJeff Roberson * Cache a pointer to the malloc_type that most recently freed 3468f70816cSJeff Roberson * this memory here. This way we know who is most likely to 3478f70816cSJeff Roberson * have stepped on it later. 3488f70816cSJeff Roberson * 3498f70816cSJeff Roberson * This code assumes that size is a multiple of 8 bytes for 3508f70816cSJeff Roberson * 64 bit machines 3518f70816cSJeff Roberson */ 3528f70816cSJeff Roberson mtp = (struct malloc_type **) 3538f70816cSJeff Roberson ((unsigned long)mtp & ~UMA_ALIGN_PTR); 3548f70816cSJeff Roberson mtp += (size - sizeof(struct malloc_type *)) / 3558f70816cSJeff Roberson sizeof(struct malloc_type *); 3568f70816cSJeff Roberson *mtp = type; 3578f70816cSJeff Roberson #endif 358099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 35914bf02f8SJohn Dyson } else { 3608355f576SJeff Roberson size = slab->us_size; 3618355f576SJeff Roberson uma_large_free(slab); 36214bf02f8SJohn Dyson } 3634362fadaSBrian Feldman malloc_type_freed(type, size); 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes 366df8bae1dSRodney W. Grimes /* 36744a8ff31SArchie Cobbs * realloc: change the size of a memory block 36844a8ff31SArchie Cobbs */ 36944a8ff31SArchie Cobbs void * 37044a8ff31SArchie Cobbs realloc(addr, size, type, flags) 37144a8ff31SArchie Cobbs void *addr; 37244a8ff31SArchie Cobbs unsigned long size; 37344a8ff31SArchie Cobbs struct malloc_type *type; 37444a8ff31SArchie Cobbs int flags; 37544a8ff31SArchie Cobbs { 3768355f576SJeff Roberson uma_slab_t slab; 37744a8ff31SArchie Cobbs unsigned long alloc; 37844a8ff31SArchie Cobbs void *newaddr; 37944a8ff31SArchie Cobbs 38044a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 38144a8ff31SArchie Cobbs if (addr == NULL) 38244a8ff31SArchie Cobbs return (malloc(size, type, flags)); 38344a8ff31SArchie Cobbs 38499571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 3858355f576SJeff Roberson 38644a8ff31SArchie Cobbs /* Sanity check */ 3878355f576SJeff Roberson KASSERT(slab != NULL, 38844a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 38944a8ff31SArchie Cobbs 39044a8ff31SArchie Cobbs /* Get the size of the original block */ 391099a0e58SBosko Milekic if (slab->us_keg) 392099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 3938355f576SJeff Roberson else 3948355f576SJeff Roberson alloc = slab->us_size; 39544a8ff31SArchie Cobbs 39644a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 39744a8ff31SArchie Cobbs if (size <= alloc 39844a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 39944a8ff31SArchie Cobbs return (addr); 40044a8ff31SArchie Cobbs 40144a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 40244a8ff31SArchie Cobbs if ((newaddr = malloc(size, type, flags)) == NULL) 40344a8ff31SArchie Cobbs return (NULL); 40444a8ff31SArchie Cobbs 40544a8ff31SArchie Cobbs /* Copy over original contents */ 40644a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 40744a8ff31SArchie Cobbs free(addr, type); 40844a8ff31SArchie Cobbs return (newaddr); 40944a8ff31SArchie Cobbs } 41044a8ff31SArchie Cobbs 41144a8ff31SArchie Cobbs /* 41244a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 41344a8ff31SArchie Cobbs */ 41444a8ff31SArchie Cobbs void * 41544a8ff31SArchie Cobbs reallocf(addr, size, type, flags) 41644a8ff31SArchie Cobbs void *addr; 41744a8ff31SArchie Cobbs unsigned long size; 41844a8ff31SArchie Cobbs struct malloc_type *type; 41944a8ff31SArchie Cobbs int flags; 42044a8ff31SArchie Cobbs { 42144a8ff31SArchie Cobbs void *mem; 42244a8ff31SArchie Cobbs 42368f2d20bSPoul-Henning Kamp if ((mem = realloc(addr, size, type, flags)) == NULL) 42444a8ff31SArchie Cobbs free(addr, type); 42544a8ff31SArchie Cobbs return (mem); 42644a8ff31SArchie Cobbs } 42744a8ff31SArchie Cobbs 42844a8ff31SArchie Cobbs /* 429df8bae1dSRodney W. Grimes * Initialize the kernel memory allocator 430df8bae1dSRodney W. Grimes */ 4312b14f991SJulian Elischer /* ARGSUSED*/ 4322b14f991SJulian Elischer static void 433d841aaa7SBruce Evans kmeminit(dummy) 434d841aaa7SBruce Evans void *dummy; 435df8bae1dSRodney W. Grimes { 4366f267175SJeff Roberson u_int8_t indx; 43727b8623fSDavid Greenman u_long mem_size; 4388355f576SJeff Roberson int i; 4398a58a9f6SJohn Dyson 4406008862bSJohn Baldwin mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 44169ef67f9SJason Evans 4428a58a9f6SJohn Dyson /* 4438a58a9f6SJohn Dyson * Try to auto-tune the kernel memory size, so that it is 4448a58a9f6SJohn Dyson * more applicable for a wider range of machine sizes. 4458a58a9f6SJohn Dyson * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while 4468a58a9f6SJohn Dyson * a VM_KMEM_SIZE of 12MB is a fair compromise. The 4478a58a9f6SJohn Dyson * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space 4488a58a9f6SJohn Dyson * available, and on an X86 with a total KVA space of 256MB, 4498a58a9f6SJohn Dyson * try to keep VM_KMEM_SIZE_MAX at 80MB or below. 4508a58a9f6SJohn Dyson * 4518a58a9f6SJohn Dyson * Note that the kmem_map is also used by the zone allocator, 4528a58a9f6SJohn Dyson * so make sure that there is enough space. 4538a58a9f6SJohn Dyson */ 454099a0e58SBosko Milekic vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE; 4551795d0cdSPaul Saab mem_size = cnt.v_page_count; 4568a58a9f6SJohn Dyson 4578a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE) 4581795d0cdSPaul Saab if ((mem_size / VM_KMEM_SIZE_SCALE) > (vm_kmem_size / PAGE_SIZE)) 4591795d0cdSPaul Saab vm_kmem_size = (mem_size / VM_KMEM_SIZE_SCALE) * PAGE_SIZE; 4608a58a9f6SJohn Dyson #endif 4618a58a9f6SJohn Dyson 4628a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX) 46381930014SPeter Wemm if (vm_kmem_size >= VM_KMEM_SIZE_MAX) 46481930014SPeter Wemm vm_kmem_size = VM_KMEM_SIZE_MAX; 4658a58a9f6SJohn Dyson #endif 4668a58a9f6SJohn Dyson 4678de6e8e1SMike Smith /* Allow final override from the kernel environment */ 46884344f9fSDag-Erling Smørgrav #ifndef BURN_BRIDGES 46984344f9fSDag-Erling Smørgrav if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0) 47084344f9fSDag-Erling Smørgrav printf("kern.vm.kmem.size is now called vm.kmem_size!\n"); 47184344f9fSDag-Erling Smørgrav #endif 47284344f9fSDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size); 4738de6e8e1SMike Smith 47427b8623fSDavid Greenman /* 47527b8623fSDavid Greenman * Limit kmem virtual size to twice the physical memory. 47627b8623fSDavid Greenman * This allows for kmem map sparseness, but limits the size 47727b8623fSDavid Greenman * to something sane. Be careful to not overflow the 32bit 47827b8623fSDavid Greenman * ints while doing the check. 47927b8623fSDavid Greenman */ 4801795d0cdSPaul Saab if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count) 48127b8623fSDavid Greenman vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE; 4828a58a9f6SJohn Dyson 48308442f8aSBosko Milekic /* 484347194c1SMike Silbersack * Tune settings based on the kernel map's size at this time. 485347194c1SMike Silbersack */ 486347194c1SMike Silbersack init_param3(vm_kmem_size / PAGE_SIZE); 487347194c1SMike Silbersack 488df8bae1dSRodney W. Grimes kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, 489099a0e58SBosko Milekic (vm_offset_t *)&kmemlimit, vm_kmem_size); 4903075778bSJohn Dyson kmem_map->system_map = 1; 4918355f576SJeff Roberson 49299571dc3SJeff Roberson uma_startup2(); 4938355f576SJeff Roberson 4946f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 4956f267175SJeff Roberson int size = kmemzones[indx].kz_size; 4966f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 4978355f576SJeff Roberson 4988efc4effSJeff Roberson kmemzones[indx].kz_zone = uma_zcreate(name, size, 4998efc4effSJeff Roberson #ifdef INVARIANTS 5008f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 5018efc4effSJeff Roberson #else 5028efc4effSJeff Roberson NULL, NULL, NULL, NULL, 5038efc4effSJeff Roberson #endif 5048efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 5056f267175SJeff Roberson 5068355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 5076f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 5088355f576SJeff Roberson 509df8bae1dSRodney W. Grimes } 510254c6cb3SPoul-Henning Kamp } 511254c6cb3SPoul-Henning Kamp 512db669378SPeter Wemm void 513db669378SPeter Wemm malloc_init(data) 514db669378SPeter Wemm void *data; 515254c6cb3SPoul-Henning Kamp { 516db669378SPeter Wemm struct malloc_type *type = (struct malloc_type *)data; 517254c6cb3SPoul-Henning Kamp 5186f267175SJeff Roberson mtx_lock(&malloc_mtx); 519d1bbc7ecSPoul-Henning Kamp if (type->ks_magic != M_MAGIC) 520d1bbc7ecSPoul-Henning Kamp panic("malloc type lacks magic"); 521d1bbc7ecSPoul-Henning Kamp 522d4060a87SJohn Dyson if (cnt.v_page_count == 0) 523d4060a87SJohn Dyson panic("malloc_init not allowed before vm init"); 524d4060a87SJohn Dyson 5256f267175SJeff Roberson if (type->ks_next != NULL) 5266f267175SJeff Roberson return; 5276f267175SJeff Roberson 528254c6cb3SPoul-Henning Kamp type->ks_next = kmemstatistics; 529254c6cb3SPoul-Henning Kamp kmemstatistics = type; 5305a34a9f0SJeff Roberson mtx_init(&type->ks_mtx, type->ks_shortdesc, "Malloc Stats", MTX_DEF); 5316f267175SJeff Roberson mtx_unlock(&malloc_mtx); 532df8bae1dSRodney W. Grimes } 533db669378SPeter Wemm 534db669378SPeter Wemm void 535db669378SPeter Wemm malloc_uninit(data) 536db669378SPeter Wemm void *data; 537db669378SPeter Wemm { 538db669378SPeter Wemm struct malloc_type *type = (struct malloc_type *)data; 539db669378SPeter Wemm struct malloc_type *t; 540db669378SPeter Wemm 5416f267175SJeff Roberson mtx_lock(&malloc_mtx); 5425a34a9f0SJeff Roberson mtx_lock(&type->ks_mtx); 543db669378SPeter Wemm if (type->ks_magic != M_MAGIC) 544db669378SPeter Wemm panic("malloc type lacks magic"); 545db669378SPeter Wemm 546db669378SPeter Wemm if (cnt.v_page_count == 0) 547db669378SPeter Wemm panic("malloc_uninit not allowed before vm init"); 548db669378SPeter Wemm 549db669378SPeter Wemm if (type == kmemstatistics) 550db669378SPeter Wemm kmemstatistics = type->ks_next; 551db669378SPeter Wemm else { 552db669378SPeter Wemm for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) { 553db669378SPeter Wemm if (t->ks_next == type) { 554db669378SPeter Wemm t->ks_next = type->ks_next; 555db669378SPeter Wemm break; 556db669378SPeter Wemm } 557db669378SPeter Wemm } 558db669378SPeter Wemm } 559ce45b512SBruce Evans type->ks_next = NULL; 5605a34a9f0SJeff Roberson mtx_destroy(&type->ks_mtx); 5616f267175SJeff Roberson mtx_unlock(&malloc_mtx); 562db669378SPeter Wemm } 5636f267175SJeff Roberson 5646f267175SJeff Roberson static int 5656f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS) 5666f267175SJeff Roberson { 5676f267175SJeff Roberson struct malloc_type *type; 5686f267175SJeff Roberson int linesize = 128; 5696f267175SJeff Roberson int curline; 5706f267175SJeff Roberson int bufsize; 5716f267175SJeff Roberson int first; 5726f267175SJeff Roberson int error; 5736f267175SJeff Roberson char *buf; 5746f267175SJeff Roberson char *p; 5756f267175SJeff Roberson int cnt; 5766f267175SJeff Roberson int len; 5776f267175SJeff Roberson int i; 5786f267175SJeff Roberson 5796f267175SJeff Roberson cnt = 0; 5806f267175SJeff Roberson 5816f267175SJeff Roberson mtx_lock(&malloc_mtx); 5826f267175SJeff Roberson for (type = kmemstatistics; type != NULL; type = type->ks_next) 5836f267175SJeff Roberson cnt++; 5846f267175SJeff Roberson 5855a34a9f0SJeff Roberson mtx_unlock(&malloc_mtx); 5866f267175SJeff Roberson bufsize = linesize * (cnt + 1); 587a163d034SWarner Losh p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 5885a34a9f0SJeff Roberson mtx_lock(&malloc_mtx); 5896f267175SJeff Roberson 5906f267175SJeff Roberson len = snprintf(p, linesize, 5916f267175SJeff Roberson "\n Type InUse MemUse HighUse Requests Size(s)\n"); 5926f267175SJeff Roberson p += len; 5936f267175SJeff Roberson 5946f267175SJeff Roberson for (type = kmemstatistics; cnt != 0 && type != NULL; 5956f267175SJeff Roberson type = type->ks_next, cnt--) { 5966f267175SJeff Roberson if (type->ks_calls == 0) 5976f267175SJeff Roberson continue; 5986f267175SJeff Roberson 5996f267175SJeff Roberson curline = linesize - 2; /* Leave room for the \n */ 600289f207cSJeff Roberson len = snprintf(p, curline, "%13s%6lu%6luK%7luK%9llu", 6016f267175SJeff Roberson type->ks_shortdesc, 6026f267175SJeff Roberson type->ks_inuse, 6036f267175SJeff Roberson (type->ks_memuse + 1023) / 1024, 6046f267175SJeff Roberson (type->ks_maxused + 1023) / 1024, 6056f267175SJeff Roberson (long long unsigned)type->ks_calls); 6066f267175SJeff Roberson curline -= len; 6076f267175SJeff Roberson p += len; 6086f267175SJeff Roberson 6096f267175SJeff Roberson first = 1; 610280759e7SRobert Drehmel for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1; 611280759e7SRobert Drehmel i++) { 6126f267175SJeff Roberson if (type->ks_size & (1 << i)) { 6136f267175SJeff Roberson if (first) 6146f267175SJeff Roberson len = snprintf(p, curline, " "); 6156f267175SJeff Roberson else 6166f267175SJeff Roberson len = snprintf(p, curline, ","); 6176f267175SJeff Roberson curline -= len; 6186f267175SJeff Roberson p += len; 6196f267175SJeff Roberson 6206f267175SJeff Roberson len = snprintf(p, curline, 6216f267175SJeff Roberson "%s", kmemzones[i].kz_name); 6226f267175SJeff Roberson curline -= len; 6236f267175SJeff Roberson p += len; 6246f267175SJeff Roberson 6256f267175SJeff Roberson first = 0; 6266f267175SJeff Roberson } 627280759e7SRobert Drehmel } 6286f267175SJeff Roberson 6296f267175SJeff Roberson len = snprintf(p, 2, "\n"); 6306f267175SJeff Roberson p += len; 6316f267175SJeff Roberson } 6326f267175SJeff Roberson 6336f267175SJeff Roberson mtx_unlock(&malloc_mtx); 6346f267175SJeff Roberson error = SYSCTL_OUT(req, buf, p - buf); 6356f267175SJeff Roberson 6366f267175SJeff Roberson free(buf, M_TEMP); 6376f267175SJeff Roberson return (error); 6386f267175SJeff Roberson } 6396f267175SJeff Roberson 6406f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD, 6416f267175SJeff Roberson NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats"); 6425e914b96SJeff Roberson 6435e914b96SJeff Roberson #ifdef MALLOC_PROFILE 6445e914b96SJeff Roberson 6455e914b96SJeff Roberson static int 6465e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 6475e914b96SJeff Roberson { 6485e914b96SJeff Roberson int linesize = 64; 6495e914b96SJeff Roberson uint64_t count; 6505e914b96SJeff Roberson uint64_t waste; 6515e914b96SJeff Roberson uint64_t mem; 6525e914b96SJeff Roberson int bufsize; 6535e914b96SJeff Roberson int error; 6545e914b96SJeff Roberson char *buf; 6555e914b96SJeff Roberson int rsize; 6565e914b96SJeff Roberson int size; 6575e914b96SJeff Roberson char *p; 6585e914b96SJeff Roberson int len; 6595e914b96SJeff Roberson int i; 6605e914b96SJeff Roberson 6615e914b96SJeff Roberson bufsize = linesize * (KMEM_ZSIZE + 1); 6625e914b96SJeff Roberson bufsize += 128; /* For the stats line */ 6635e914b96SJeff Roberson bufsize += 128; /* For the banner line */ 6645e914b96SJeff Roberson waste = 0; 6655e914b96SJeff Roberson mem = 0; 6665e914b96SJeff Roberson 667a163d034SWarner Losh p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 6685e914b96SJeff Roberson len = snprintf(p, bufsize, 6695e914b96SJeff Roberson "\n Size Requests Real Size\n"); 6705e914b96SJeff Roberson bufsize -= len; 6715e914b96SJeff Roberson p += len; 6725e914b96SJeff Roberson 6735e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 6745e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 6755e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 6765e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 6775e914b96SJeff Roberson 6785e914b96SJeff Roberson len = snprintf(p, bufsize, "%6d%28llu%11d\n", 6795e914b96SJeff Roberson size, (unsigned long long)count, rsize); 6805e914b96SJeff Roberson bufsize -= len; 6815e914b96SJeff Roberson p += len; 6825e914b96SJeff Roberson 6835e914b96SJeff Roberson if ((rsize * count) > (size * count)) 6845e914b96SJeff Roberson waste += (rsize * count) - (size * count); 6855e914b96SJeff Roberson mem += (rsize * count); 6865e914b96SJeff Roberson } 6875e914b96SJeff Roberson 6885e914b96SJeff Roberson len = snprintf(p, bufsize, 6895e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 6905e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 6915e914b96SJeff Roberson p += len; 6925e914b96SJeff Roberson 6935e914b96SJeff Roberson error = SYSCTL_OUT(req, buf, p - buf); 6945e914b96SJeff Roberson 6955e914b96SJeff Roberson free(buf, M_TEMP); 6965e914b96SJeff Roberson return (error); 6975e914b96SJeff Roberson } 6985e914b96SJeff Roberson 6995e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 7005e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 7015e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 702