1.\" $OpenBSD: malloc.9,v 1.42 2008/09/02 21:42:05 chl 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: September 2 2008 $ 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 58Unlike its standard C library counterpart 59.Pq Xr malloc 3 , 60the kernel version takes two more arguments. 61The 62.Fa flags 63argument further qualifies 64.Fn malloc Ns 's 65operational characteristics as follows: 66.Bl -tag -width xxx -offset indent 67.It Dv M_WAITOK 68This is defined to be 0, and is therefore most useful as an aid to code 69readability. 70In effect, it is the same as having no other 71.Fa flags 72specified. 73If memory is currently unavailable, 74.Fn malloc 75may call sleep to wait for resources to be released by other processes. 76.It Dv M_NOWAIT 77Causes 78.Fn malloc 79to return 80.Dv NULL 81if the request cannot be immediately fulfilled due to resource shortage. 82.It Dv M_CANFAIL 83In the 84.Dv M_WAITOK 85case, if not enough memory is available, return 86.Dv NULL 87instead of calling 88.Xr panic 9 . 89.Dv M_CANFAIL 90has no effect if 91.Dv M_NOWAIT 92is specified. 93.It Dv M_ZERO 94Causes 95.Fn malloc 96to return zeroed memory. 97.El 98.Pp 99The 100.Fa type 101argument broadly identifies the kernel subsystem for which the allocated 102memory was needed, and is commonly used to maintain statistics about 103kernel memory usage. 104The following types are currently defined: 105.Pp 106.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact 107.It Dv M_FREE 108Should be on free list. 109.It Dv M_MBUF 110Mbuf memory. 111.It Dv M_DEVBUF 112Device driver memory. 113.It Dv M_DEBUG 114.Nm malloc 115debug structures. 116.It Dv M_PCB 117Protocol control blocks. 118.It Dv M_RTABLE 119Routing tables. 120.It Dv M_FTABLE 121Fragment reassembly headers. 122.It Dv M_IFADDR 123Interface addresses. 124.It Dv M_SOOPTS 125Socket options. 126.It Dv M_SYSCTL 127Sysctl persistent buffers. 128.It Dv M_IOCTLOPS 129Ioctl data buffers. 130.It Dv M_IOV 131Large IOVs. 132.It Dv M_MOUNT 133VFS mount structs. 134.It Dv M_NFSREQ 135NFS request headers. 136.It Dv M_NFSMNT 137NFS mount structures. 138.It Dv M_NFSNODE 139NFS vnode private part. 140.It Dv M_VNODE 141Dynamically allocated vnodes. 142.It Dv M_CACHE 143Dynamically allocated cache entries. 144.It Dv M_DQUOT 145UFS quota entries. 146.It Dv M_UFSMNT 147UFS mount structures. 148.It Dv M_SHM 149SVID compatible shared memory segments. 150.It Dv M_VMMAP 151VM map structures. 152.It Dv M_SEM 153SVID compatible semaphores. 154.It Dv M_DIRHASH 155UFS directory hash structures. 156.It Dv M_VMPMAP 157VM pmap data. 158.It Dv M_FILE 159Open file structures. 160.It Dv M_FILEDESC 161Open file descriptor tables. 162.It Dv M_PROC 163Proc structures. 164.It Dv M_SUBPROC 165Proc sub-structures. 166.It Dv M_VCLUSTER 167Cluster for VFS. 168.It Dv M_MFSNODE 169MFS vnode private part. 170.It Dv M_NETADDR 171Export host address structures. 172.It Dv M_NFSSVC 173NFS server structures. 174.It Dv M_NFSUID 175NFS uid mapping structures. 176.It Dv M_NFSD 177NFS server daemon structures. 178.It Dv M_IPMOPTS 179Internet multicast options. 180.It Dv M_IPMADDR 181Internet multicast addresses. 182.It Dv M_IFMADDR 183Link-level multicast addresses. 184.It Dv M_MRTABLE 185Multicast routing tables. 186.It Dv M_ISOFSMNT 187ISOFS mount structures. 188.It Dv M_ISOFSNODE 189ISOFS vnode private part. 190.It Dv M_MSDOSFSMNT 191MSDOS FS mount structures. 192.It Dv M_MSDOSFSFAT 193MSDOS FS FAT tables. 194.It Dv M_MSDOSFSNODE 195MSDOS FS vnode private part. 196.It Dv M_TTYS 197Allocated tty structures. 198.It Dv M_EXEC 199Argument lists & other mem used by exec. 200.It Dv M_MISCFSMNT 201Misc. FS mount structures. 202.It Dv M_PFKEY 203Pfkey data. 204.It Dv M_TDB 205Transforms database. 206.It Dv M_XDATA 207IPsec data. 208.It Dv M_PAGEDEP 209File page dependencies. 210.It Dv M_INODEDEP 211Inode dependencies. 212.It Dv M_NEWBLK 213New block allocation. 214.It Dv M_RAIDFRAME 215RAIDframe data. 216.It Dv M_UVMAMAP 217UVM amap and related. 218.It Dv M_UVMAOBJ 219UVM aobj and related. 220.It Dv M_USB 221USB general. 222.It Dv M_USBDEV 223USB device driver. 224.It Dv M_USBHC 225USB host controller. 226.It Dv M_MEMDESC 227Memory range. 228.It Dv M_CRYPTO_DATA 229.Xr crypto 4 230data buffers. 231.It Dv M_CREDENTIALS 232.Xr ipsec 4 233related credentials. 234.It Dv M_PACKET_TAGS 235Packet-attached information tags. 236.It Dv M_1394CTL 237IEEE 1394 control structures. 238.It Dv M_1394DATA 239IEEE 1394 data buffers. 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_IP6RR 247IPv6 router renumbering prefix. 248.It Dv M_RR_ADDR 249IPv6 router renumbering interface identifiers. 250.It Dv M_TEMP 251Miscellaneous temporary data buffers. 252.It Dv M_NTFSMNT 253NTFS mount structures. 254.It Dv M_NTFSNTNODE 255NTFS ntnode information. 256.It Dv M_NTFSNODE 257NTFS fnode information. 258.It Dv M_NTFSDIR 259NTFS directory buffers. 260.It Dv M_NTFSHASH 261NTFS ntnode hash tables. 262.It Dv M_NTFSVATTR 263NTFS file attribute information. 264.It Dv M_NTFSRDATA 265NTFS resident data. 266.It Dv M_NTFSDECOMP 267NTFS decompression temporary storage. 268.It Dv M_NTFSRUN 269NTFS vrun storage. 270.It Dv M_KEVENT 271.Xr kqueue 2 272data structures. 273.It Dv M_BLUETOOTH 274Bluetooth data structures. 275.It Dv M_BWMETER 276Multicast upcall bandwidth meters. 277.It Dv M_UDFMOUNT 278UDF mount structures. 279.It Dv M_UDFFENTRY 280UDF file entries. 281.It Dv M_UDFFID 282UDF file ids. 283.El 284.Pp 285Statistics based on the 286.Fa type 287argument are maintained only if the kernel option 288.Dv KMEMSTATS 289is used when compiling the kernel 290.Po the default in current\ \& 291.Ox 292kernels 293.Pc 294and can be examined by using 295.Sq vmstat -m . 296.Sh RETURN VALUES 297.Fn malloc 298returns a kernel virtual address that is suitably aligned for storage of 299any type of object. 300.Sh DIAGNOSTICS 301A kernel compiled with the 302.Dv DIAGNOSTIC 303configuration option attempts to detect memory corruption caused by 304such things as writing outside the allocated area and unbalanced calls to the 305.Fn malloc 306and 307.Fn free 308functions. 309Failing consistency checks will cause a panic or a system console message: 310.Bl -bullet -offset indent -compact 311.Pp 312.It 313panic: 314.Dq malloc - bogus type 315.It 316panic: 317.Dq malloc: out of space in kmem_map 318.It 319panic: 320.Dq malloc: allocation too large 321.It 322panic: 323.Dq malloc: wrong bucket 324.It 325panic: 326.Dq malloc: lost data 327.It 328panic: 329.Dq free: unaligned addr 330.It 331panic: 332.Dq free: duplicated free 333.It 334panic: 335.Dq free: multiple frees 336.It 337panic: 338.Dq kmeminit: minbucket too small/struct freelist too big 339.It 340.Dq multiply freed item Aq addr 341.It 342.Dq Data modified on freelist: Aq data object description 343.El 344.Sh DEBUGGING 345A kernel compiled with the 346.Cm MALLOC_DEBUG 347option allows for more extensive debugging of memory allocations. 348The 349.Va debug_malloc_type , 350.Va debug_malloc_size , 351.Va debug_malloc_size_lo 352and 353.Va debug_malloc_size_hi 354variables choose which allocation to debug. 355.Va debug_malloc_type 356should be set to the memory type and 357.Va debug_malloc_size 358should be set to the memory size to debug. 3590 can be used as a wildcard. 360.Va debug_malloc_size_lo 361and 362.Va debug_malloc_size_hi 363can be used to specify a range of sizes if the exact size to debug is not 364known. 365When those are used, 366.Va debug_malloc_size 367needs to be set to the wildcard. 368.Dv M_DEBUG 369can also be specified as an allocation type to force allocation with 370debugging. 371.Pp 372Every call to 373.Fn malloc 374with a memory type and size that matches the debugged type and size will 375allocate two virtual pages. 376The pointer returned will be aligned so that 377the requested area will end at the page boundary and the second virtual page 378will be left unmapped. 379This way we can catch reads and writes outside the allocated area. 380.Pp 381Every call to 382.Fn free 383with memory that was returned by the debugging malloc will cause the memory 384area to become unmapped so that we can catch dangling reads and writes to 385freed memory. 386.Pp 387There are no special diagnostics if any errors are caught by the debugging 388malloc. 389The errors will look like normal access to unmapped memory. 390On a memory access error, the 391.Ic show malloc 392command in 393.Xr ddb 4 394can be invoked to see what memory areas are allocated and freed. 395If the faulting address is within two pages from an address on the allocated 396list, there was an access outside the allocated area. 397If the faulting address is within two pages from an address on the free list, 398there was an access to freed memory. 399.Pp 400Care needs to be taken when using the 401.Cm MALLOC_DEBUG 402option: the memory consumption can run away pretty quickly and there is 403a severe performance degradation when allocating and freeing debugged memory 404types. 405.Sh SEE ALSO 406.Xr vmstat 8 407