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