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