1.\" $OpenBSD: malloc.9,v 1.18 2001/07/17 15:52:56 art 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.\" 3. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the NetBSD 21.\" Foundation, Inc. and its contributors. 22.\" 4. Neither the name of The NetBSD Foundation nor the names of its 23.\" contributors may be used to endorse or promote products derived 24.\" from this software without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE 30.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36.\" POSSIBILITY OF SUCH DAMAGE. 37.\" 38.Dd June 16, 1996 39.Dt MALLOC 9 40.Os 41.Sh NAME 42.Nm malloc 43.Nd kernel memory allocator 44.Sh SYNOPSIS 45.Fd #include <sys/types.h> 46.Fd #include <sys/malloc.h> 47.Ft void * 48.Fn malloc "unsigned long size" "int type" "int flags" 49.Fn MALLOC "space" "cast" "unsigned long size" "int type" "int flags" 50.Ft void 51.Fn free "void *addr" "int type" 52.Fn FREE "void *addr" "int type" 53.Sh DESCRIPTION 54The 55.Fn malloc 56function allocates uninitialized memory in kernel address space for an 57object whose size is specified by 58.Fa size . 59.Fn free 60releases memory at address 61.Fa addr 62that was previously allocated by 63.Fn malloc 64for re-use. 65The 66.Fn MALLOC 67macro variant is functionally equivalent to 68.Bd -literal -offset indent 69(space) = (cast)malloc((u_long)(size), type, flags) 70.Ed 71.Pp 72and the 73.Fn FREE 74macro variant is equivalent to 75.Bd -literal -offset indent 76free((caddr_t)(addr), type) 77.Ed 78.Pp 79These macros should only be used when the 80.Fa size 81argument is a constant. 82.Pp 83Unlike its standard C library counterpart 84.Pq Xr malloc 3 , 85the kernel version takes two more arguments. 86The 87.Fa flags 88argument further qualifies 89.Fn malloc No Ns 's 90operational characteristics as follows: 91.Bl -tag -width xxx -offset indent 92.It Dv M_NOWAIT 93Causes 94.Fn malloc 95to return 96.Dv NULL 97if the request cannot be immediately fulfilled due to resource shortage. 98Otherwise, 99.Fn malloc 100may call sleep to wait for resources to be released by other processes. 101If this flag is not set, 102.Fn malloc 103will never return 104.Dv NULL . 105Note that 106.Dv M_WAITOK 107is conveniently defined to be 0, and hence maybe or'ed into the 108.Fa flags 109argument to indicate that it's Ok to wait for resources. 110.El 111.Pp 112Currently, only one flag is defined. 113.Pp 114The 115.Fa type 116argument broadly identifies the kernel subsystem for which the allocated 117memory was needed, and is commonly used to maintain statistics about 118kernel memory usage. 119The following types are currently defined: 120.Pp 121.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact 122.It Dv M_FREE 123Should be on free list. 124.It Dv M_MBUF 125Mbuf memory. 126.It Dv M_DEVBUF 127Device driver memory. 128.It Dv M_SOCKET 129Socket structure. 130.It Dv M_PCB 131Protocol control block. 132.It Dv M_RTABLE 133Routing tables. 134.It Dv M_HTABLE 135IMP host tables. 136.It Dv M_FTABLE 137Fragment reassembly header. 138.It Dv M_ZOMBIE 139Zombie proc status 140.It Dv M_IFADDR 141Interface address. 142.It Dv M_SOOPTS 143Socket options. 144.It Dv M_SYSCTL 145Sysctl persistent buffers. 146.It Dv M_NAMEI 147Namei path name buffer. 148.It Dv M_GPROF 149Kernel profiling buffer. 150.It Dv M_IOCTLOPS 151Ioctl data buffer. 152.It Dv M_CRED 153Credentials. 154.It Dv M_PGRP 155Process group header. 156.It Dv M_SESSION 157Session header. 158.It Dv M_IOV 159Large IOVs. 160.It Dv M_MOUNT 161Vfs mount struct. 162.It Dv M_NFSREQ 163NFS request header. 164.It Dv M_NFSMNT 165NFS mount structure. 166.It Dv M_NFSNODE 167NFS vnode private part. 168.It Dv M_VNODE 169Dynamically allocated vnodes. 170.It Dv M_CACHE 171Dynamically allocated cache entries. 172.It Dv M_DQUOT 173UFS quota entries. 174.It Dv M_UFSMNT 175UFS mount structure. 176.It Dv M_SHM 177SVID compatible shared memory segments. 178.It Dv M_VMMAP 179VM map structures. 180.It Dv M_VMMAPENT 181VM map entry structures. 182.It Dv M_VMOBJ 183VM object structure. 184.It Dv M_VMOBJHASH 185VM object hash structure. 186.It Dv M_VMPMAP 187VM pmap. 188.It Dv M_VMPVENT 189VM phys-virt mapping entry. 190.It Dv M_VMPAGER 191XXX: VM pager struct. 192.It Dv M_VMPGDATA 193XXX: VM pager private data. 194.It Dv M_FILE 195Open file structure. 196.It Dv M_FILEDESC 197Open file descriptor table. 198.It Dv M_LOCKF 199Byte-range locking structures. 200.It Dv M_PROC 201Proc structures. 202.It Dv M_SUBPROC 203Proc sub-structures. 204.It Dv M_SEGMENT 205Segment for LFS. 206.It Dv M_LFSNODE 207LFS vnode private part. 208.It Dv M_FFSNODE 209FFS vnode private part. 210.It Dv M_MFSNODE 211MFS vnode private part. 212.It Dv M_NQLEASE 213Nqnfs lease. 214.It Dv M_NQMHOST 215Nqnfs host address table. 216.It Dv M_NETADDR 217Export host address structure. 218.It Dv M_NFSSVC 219Nfs server structure. 220.It Dv M_NFSUID 221Nfs uid mapping structure. 222.It Dv M_NFSD 223Nfs server daemon structure. 224.It Dv M_IPMOPTS 225Internet multicast options. 226.It Dv M_IPMADDR 227Internet multicast address. 228.It Dv M_IFMADDR 229Link-level multicast address. 230.It Dv M_MRTABLE 231Multicast routing tables. 232.It Dv M_ISOFSMNT 233ISOFS mount structure. 234.It Dv M_ISOFSNODE 235ISOFS vnode private part. 236.It Dv M_MSDOSFSMNT 237MSDOS FS mount structure. 238.It Dv M_MSDOSFSFAT 239MSDOS FS fat table. 240.It Dv M_MSDOSFSNODE 241MSDOS FS vnode private part. 242.It Dv M_TTYS 243Allocated tty structures. 244.It Dv M_EXEC 245Argument lists & other mem used by exec. 246.It Dv M_MISCFSMNT 247Miscfs mount structures. 248.It Dv M_ADOSFSMNT 249Adosfs mount structures. 250.It Dv M_ANODE 251Adosfs anode structures and tables. 252.It Dv M_IPQ 253IP packet queue entry. 254.It Dv M_AFS 255Andrew File System. 256.It Dv M_ADOSFSBITMAP 257Adosfs bitmap. 258.It Dv M_EXT2FSNODE 259EXT2FS vnode private part. 260.It Dv M_PFIL 261Packer filter. 262.It Dv M_PFKEY 263Pfkey data. 264.It Dv M_TDB 265Transforms database. 266.It Dv M_XDATA 267IPsec data. 268.It Dv M_VFS 269VFS file systems. 270.It Dv M_PAGEDEP 271File page dependencies. 272.It Dv M_INODEDEP 273Inode dependencies. 274.It Dv M_NEWBLK 275New block allocation. 276.It Dv M_BMSAFEMAP 277Block or frag allocated from cyl group map. 278.It Dv M_ALLOCDIRECT 279Block or frag dependency for an inode. 280.It Dv M_INDIRDEP 281Indirect block dependencies. 282.It Dv M_ALLOCINDIR 283Block dependency for an indirect block. 284.It Dv M_FREEFRAG 285Previously used frag for an inode. 286.It Dv M_FREEBLKS 287Blocks freed from an inode. 288.It Dv M_FREEFILE 289Inode deallocated. 290.It Dv M_DIRADD 291New directory entry. 292.It Dv M_MKDIR 293New directory. 294.It Dv M_DIRREM 295Directory entry deleted. 296.It Dv M_VMPBUCKET 297VM page buckets. 298.It Dv M_VMSWAP 299VM swap structures. 300.It Dv M_RAIDFRAME 301Raidframe data. 302.It Dv M_UVMAMAP 303UVM amap and realted. 304.It Dv M_UVMAOBJ 305UVM aobj and realted. 306.It Dv M_POOL 307Pool memory. 308.It Dv M_USB 309USB general. 310.It Dv M_USBDEV 311USB device driver. 312.It Dv M_USBHC 313USB host controller. 314.It Dv M_PIPE 315Pipe structures. 316.It Dv M_MEMDESC 317Memory range. 318.It Dv M_DEBUG 319.Xr malloc 9 320debug structures. 321.It Dv M_KNOTE 322Kernel event queue. 323.It Dv M_CRYPTO_DATA 324Cryptographic framework data buffers. 325.It Dv M_IPSEC_POLICY 326.Xr ipsec 4 327SPD structures. 328.It Dv M_CREDENTIALS 329.Xr ipsec 4 330related credentials. 331.It Dv M_PACKET_TAGS 332Packet-attached information tags. 333.It Dv M_CRYPTO_OPS 334Cryptographic framework operation request structures. 335.It Dv M_IP6OPT 336IPv6 options. 337.It Dv M_IP6NDP 338IPv6 neighbour discovery structures. 339.It Dv M_IP6RR 340IPv6 router renumbering prefix. 341.It Dv M_RR_ADDR 342IPv6 router renumbering interface identifiers. 343.It Dv M_TEMP 344Miscellaneous temporary data buffers. 345.El 346.Pp 347Statistics based on the 348.Fa type 349argument are maintained only if the kernel option 350.Dv KMEMSTATS 351is used when compiling the kernel 352.Po the default in current\ \& 353.Ox 354kernels 355.Pc 356and can be examined by using 357.Sq vmstat -m . 358.Sh RETURN VALUES 359.Fn malloc 360returns a kernel virtual address that is suitably aligned for storage of 361any type of object. 362.Sh SEE ALSO 363.Xr vmstat 8 364.Sh DIAGNOSTICS 365A kernel compiled with the 366.Dv DIAGNOSTIC 367configuration option attempts to detect memory corruption caused by 368such things as writing outside the allocated area and unbalanced calls to the 369.Fn malloc 370and 371.Fn free 372functions. 373Failing consistency checks will cause a panic or a system console message: 374.Bl -bullet -offset indent -compact 375.Pp 376.It 377panic: 378.Dq malloc - bogus type 379.It 380panic: 381.Dq malloc: out of space in kmem_map 382.It 383panic: 384.Dq malloc: allocation too large 385.It 386panic: 387.Dq malloc: wrong bucket 388.It 389panic: 390.Dq malloc: lost data 391.It 392panic: 393.Dq free: unaligned addr 394.It 395panic: 396.Dq free: duplicated free 397.It 398panic: 399.Dq free: multiple frees 400.It 401panic: 402.Dq kmeminit: minbucket too small/struct freelist too big 403.It 404.Dq multiply freed item Aq addr 405.It 406.Dq Data modified on freelist: Aq data object description 407.El 408.Sh DEBUGGING 409A kernel compiled with the 410.Cm MALLOC_DEBUG 411option allows for more extensive 412debugging of memory allocations. 413You can choose which allocation to debug with the 414.Va malloc_deb_type , 415.Va malloc_deb_size , 416.Va malloc_deb_size_lo 417and 418.Va malloc_deb_size_hi 419variables. 420.Va malloc_deb_type 421should be set to the memory type and 422.Va malloc_deb_size 423should be set to the memory size you want to debug. 4240 can be used as a wildcard. 425.Va malloc_deb_size_lo 426and 427.Va malloc_deb_size_hi 428Can be used to specify a range of sizes if the exact size to debug is not 429known. 430When those are used, you need to set 431.Va malloc_deb_size 432to the wildcard. 433.Pp 434Every call to 435.Fn malloc 436with a memory type and size that matches the debugged type and size will 437allocate two virtual pages. 438The pointer returned will be aligned so that 439the requested area will end at the page boundary and the second virtual page 440will be left unmapped. 441This way we can catch reads and writes outside the allocated area. 442.Pp 443Every call to 444.Fn free 445with memory that was returned by the debugging malloc will cause the memory 446area to become unmapped so that we can catch dangling reads and writes to 447freed memory. 448.Pp 449There are no special diagnostics if any errors are caught by the debugging 450malloc. 451The errors will look like normal access to unmapped memory. 452When you get a memory access error, you can invoke the 453.Ic show malloc 454command in 455.Xr ddb 4 456to see what memory areas are allocated and freed. 457If the faulting address is within two pages from an address on the allocated 458list, you have gotten an access outside the allocated area. 459If the faulting address is within two pages from an address on the free list, 460you have gotten an access to freed memory. 461.Pp 462You have to be very careful when using the 463.Cm MALLOC_DEBUG 464option: the memory consumption can run away pretty quickly and there is 465a severe performance degradation when allocating and freeing debugged memory 466types. 467