1.\" $OpenBSD: malloc.9,v 1.47 2012/04/06 15:42:56 jsing Exp $ 2.\" $NetBSD: malloc.9,v 1.2 1996/10/30 05:29:54 lukem Exp $ 3.\" 4.\" Copyright (c) 1996 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Paul Kranenburg. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 23.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: April 6 2012 $ 32.Dt MALLOC 9 33.Os 34.Sh NAME 35.Nm malloc , 36.Nm free 37.Nd kernel memory allocator 38.Sh SYNOPSIS 39.Fd #include <sys/types.h> 40.Fd #include <sys/malloc.h> 41.Ft void * 42.Fn malloc "unsigned long size" "int type" "int flags" 43.Ft void 44.Fn free "void *addr" "int type" 45.Sh DESCRIPTION 46The 47.Fn malloc 48function allocates uninitialized memory in kernel address space for an 49object whose size is specified by 50.Fa size . 51.Fn free 52releases memory at address 53.Fa addr 54that was previously allocated by 55.Fn malloc 56for re-use. 57.Pp 58The 59.Fa flags 60argument further qualifies malloc's 61operational characteristics as follows: 62.Bl -tag -width xxx -offset indent 63.It Dv M_WAITOK 64The same as having no other 65.Fa flags 66specified. 67If memory is currently unavailable, 68.Fn malloc 69may call sleep to wait for resources to be released by other processes. 70.It Dv M_NOWAIT 71Causes 72.Fn malloc 73to return 74.Dv NULL 75if the request cannot be immediately fulfilled due to resource shortage. 76One of 77.Dv M_NOWAIT 78or 79.Dv M_WAITOK 80must be specified. 81.It Dv M_CANFAIL 82In the 83.Dv M_WAITOK 84case, if not enough memory is available, return 85.Dv NULL 86instead of calling 87.Xr panic 9 . 88.Dv M_CANFAIL 89has no effect if 90.Dv M_NOWAIT 91is specified. 92.It Dv M_ZERO 93Causes 94.Fn malloc 95to return zeroed memory. 96.El 97.Pp 98The 99.Fa type 100argument broadly identifies the kernel subsystem for which the allocated 101memory was needed, and is commonly used to maintain statistics about 102kernel memory usage. 103These statistics can be examined using 104.Xr vmstat 8 105or 106.Xr systat 1 107if either of the kernel 108.Xr options 4 109.Cm KMEMSTATS 110or 111.Cm DEBUG 112are enabled. 113.Pp 114The following types are currently defined: 115.Pp 116.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact 117.It Dv M_FREE 118Should be on free list. 119.It Dv M_DEVBUF 120Device driver memory. 121.It Dv M_DEBUG 122.Nm malloc 123debug structures. 124.It Dv M_PCB 125Protocol control blocks. 126.It Dv M_RTABLE 127Routing tables. 128.It Dv M_FTABLE 129Fragment reassembly headers. 130.It Dv M_IFADDR 131Interface addresses. 132.It Dv M_SOOPTS 133Socket options. 134.It Dv M_SYSCTL 135Sysctl persistent buffers. 136.It Dv M_IOCTLOPS 137Ioctl data buffers. 138.It Dv M_IOV 139Large IOVs. 140.It Dv M_MOUNT 141VFS mount structs. 142.It Dv M_NFSREQ 143NFS request headers. 144.It Dv M_NFSMNT 145NFS mount structures. 146.It Dv M_VNODE 147Dynamically allocated vnodes. 148.It Dv M_CACHE 149Dynamically allocated cache entries. 150.It Dv M_DQUOT 151UFS quota entries. 152.It Dv M_UFSMNT 153UFS mount structures. 154.It Dv M_SHM 155SVID compatible shared memory segments. 156.It Dv M_VMMAP 157VM map structures. 158.It Dv M_SEM 159SVID compatible semaphores. 160.It Dv M_DIRHASH 161UFS directory hash structures. 162.It Dv M_ACPI 163ACPI structures. 164.It Dv M_VMPMAP 165VM pmap data. 166.It Dv M_FILE 167Open file structures. 168.It Dv M_FILEDESC 169Open file descriptor tables. 170.It Dv M_PROC 171Proc structures. 172.It Dv M_SUBPROC 173Proc sub-structures. 174.It Dv M_VCLUSTER 175Cluster for VFS. 176.It Dv M_MFSNODE 177MFS vnode private part. 178.It Dv M_NETADDR 179Export host address structures. 180.It Dv M_NFSSVC 181NFS server structures. 182.It Dv M_NFSD 183NFS server daemon structures. 184.It Dv M_IPMOPTS 185Internet multicast options. 186.It Dv M_IPMADDR 187Internet multicast addresses. 188.It Dv M_IFMADDR 189Link-level multicast addresses. 190.It Dv M_MRTABLE 191Multicast routing tables. 192.It Dv M_ISOFSMNT 193ISOFS mount structures. 194.It Dv M_ISOFSNODE 195ISOFS vnode private part. 196.It Dv M_MSDOSFSMNT 197MSDOS FS mount structures. 198.It Dv M_MSDOSFSFAT 199MSDOS FS FAT tables. 200.It Dv M_MSDOSFSNODE 201MSDOS FS vnode private part. 202.It Dv M_TTYS 203Allocated tty structures. 204.It Dv M_EXEC 205Argument lists & other mem used by exec. 206.It Dv M_MISCFSMNT 207Miscellaneous FS mount structures. 208.It Dv M_PFKEY 209Pfkey data. 210.It Dv M_TDB 211Transforms database. 212.It Dv M_XDATA 213IPsec data. 214.It Dv M_PAGEDEP 215File page dependencies. 216.It Dv M_INODEDEP 217Inode dependencies. 218.It Dv M_NEWBLK 219New block allocation. 220.It Dv M_UVMAMAP 221UVM amap and related. 222.It Dv M_UVMAOBJ 223UVM aobj and related. 224.It Dv M_USB 225USB general. 226.It Dv M_USBDEV 227USB device driver. 228.It Dv M_USBHC 229USB host controller. 230.It Dv M_MEMDESC 231Memory range. 232.It Dv M_CRYPTO_DATA 233.Xr crypto 4 234data buffers. 235.It Dv M_CREDENTIALS 236.Xr ipsec 4 237related credentials. 238.It Dv M_PACKET_TAGS 239Packet-attached information tags. 240.It Dv M_EMULDATA 241Per process emulation data. 242.It Dv M_IP6OPT 243IPv6 options. 244.It Dv M_IP6NDP 245IPv6 neighbour discovery structures. 246.It Dv M_TEMP 247Miscellaneous temporary data buffers. 248.It Dv M_NTFSMNT 249NTFS mount structures. 250.It Dv M_NTFSNTNODE 251NTFS ntnode information. 252.It Dv M_NTFSNODE 253NTFS fnode information. 254.It Dv M_NTFSDIR 255NTFS directory buffers. 256.It Dv M_NTFSHASH 257NTFS ntnode hash tables. 258.It Dv M_NTFSVATTR 259NTFS file attribute information. 260.It Dv M_NTFSRDATA 261NTFS resident data. 262.It Dv M_NTFSDECOMP 263NTFS decompression temporary storage. 264.It Dv M_NTFSRUN 265NTFS vrun storage. 266.It Dv M_KEVENT 267.Xr kqueue 2 268data structures. 269.It Dv M_BLUETOOTH 270Bluetooth data structures. 271.It Dv M_BWMETER 272Multicast upcall bandwidth meters. 273.It Dv M_UDFMOUNT 274UDF mount structures. 275.It Dv M_UDFFENTRY 276UDF file entries. 277.It Dv M_UDFFID 278UDF file ID. 279.It Dv M_BTHIDEV 280Bluetooth HID. 281.It Dv M_AGP 282AGP memory. 283.It Dv M_DRM 284Direct Rendering Manager. 285.El 286.Sh RETURN VALUES 287.Fn malloc 288returns a kernel virtual address that is suitably aligned for storage of 289any type of object. 290.Sh DIAGNOSTICS 291A kernel compiled with the 292.Dv DIAGNOSTIC 293configuration option attempts to detect memory corruption caused by 294such things as writing outside the allocated area and unbalanced calls to the 295.Fn malloc 296and 297.Fn free 298functions. 299Failing consistency checks will cause a panic or a system console message: 300.Pp 301.Bl -bullet -offset indent -compact 302.It 303panic: 304.Dq malloc - bogus type 305.It 306panic: 307.Dq malloc: out of space in kmem_map 308.It 309panic: 310.Dq malloc: allocation too large 311.It 312panic: 313.Dq malloc: wrong bucket 314.It 315panic: 316.Dq malloc: lost data 317.It 318panic: 319.Dq free: unaligned addr 320.It 321panic: 322.Dq free: duplicated free 323.It 324panic: 325.Dq free: multiple frees 326.It 327panic: 328.Dq kmeminit: minbucket too small/struct freelist too big 329.It 330.Dq multiply freed item Aq addr 331.It 332.Dq Data modified on freelist: Aq data object description 333.El 334.Sh DEBUGGING 335A kernel compiled with the 336.Cm MALLOC_DEBUG 337option allows for more extensive debugging of memory allocations. 338The 339.Va debug_malloc_type , 340.Va debug_malloc_size , 341.Va debug_malloc_size_lo 342and 343.Va debug_malloc_size_hi 344variables choose which allocation to debug. 345.Va debug_malloc_type 346should be set to the memory type and 347.Va debug_malloc_size 348should be set to the memory size to debug. 3490 can be used as a wildcard. 350.Va debug_malloc_size_lo 351and 352.Va debug_malloc_size_hi 353can be used to specify a range of sizes if the exact size to debug is not 354known. 355When those are used, 356.Va debug_malloc_size 357needs to be set to the wildcard. 358.Dv M_DEBUG 359can also be specified as an allocation type to force allocation with 360debugging. 361.Pp 362Every call to 363.Fn malloc 364with a memory type and size that matches the debugged type and size will 365allocate two virtual pages. 366The pointer returned will be aligned so that 367the requested area will end at the page boundary and the second virtual page 368will be left unmapped. 369This way we can catch reads and writes outside the allocated area. 370.Pp 371Every call to 372.Fn free 373with memory that was returned by the debugging malloc will cause the memory 374area to become unmapped so that we can catch dangling reads and writes to 375freed memory. 376.Pp 377There are no special diagnostics if any errors are caught by the debugging 378malloc. 379The errors will look like normal access to unmapped memory. 380On a memory access error, the 381.Ic show malloc 382command in 383.Xr ddb 4 384can be invoked to see what memory areas are allocated and freed. 385If the faulting address is within two pages from an address on the allocated 386list, there was an access outside the allocated area. 387If the faulting address is within two pages from an address on the free list, 388there was an access to freed memory. 389.Pp 390Care needs to be taken when using the 391.Cm MALLOC_DEBUG 392option: the memory consumption can run away pretty quickly and there is 393a severe performance degradation when allocating and freeing debugged memory 394types. 395.Sh SEE ALSO 396.Xr systat 1 , 397.Xr vmstat 8 398