xref: /openbsd-src/share/man/man9/malloc.9 (revision 8550894424f8a4aa4aafb6cd57229dd6ed7cd9dd)
1.\"	$OpenBSD: malloc.9,v 1.68 2022/02/03 17:18:22 guenther 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: February 3 2022 $
32.Dt MALLOC 9
33.Os
34.Sh NAME
35.Nm malloc ,
36.Nm mallocarray ,
37.Nm free
38.Nd kernel memory allocator
39.Sh SYNOPSIS
40.In sys/types.h
41.In sys/malloc.h
42.Ft void *
43.Fn malloc "size_t size" "int type" "int flags"
44.Ft void *
45.Fn mallocarray "size_t nmemb" "size_t size" "int type" "int flags"
46.Ft void
47.Fn free "void *addr" "int type" "size_t size"
48.Sh DESCRIPTION
49The
50.Fn malloc
51function allocates uninitialized memory in kernel address space for an
52object whose size is specified by
53.Fa size .
54.Pp
55The
56.Fn mallocarray
57function is the same as
58.Fn malloc ,
59but allocates space for an array of
60.Fa nmemb
61objects and checks for arithmetic overflow.
62.Pp
63The
64.Fn free
65function releases memory at address
66.Fa addr
67that was previously allocated by
68.Fn malloc
69or
70.Fn mallocarray
71for re-use.
72The same object size originally provided to
73.Fn malloc
74should be specified by
75.Fa size ,
76because
77.Fn free
78will operate faster knowing this.
79If tracking the size is difficult, specify
80.Ar size
81as 0.
82If
83.Fa addr
84is a null pointer, no action occurs.
85.Pp
86The
87.Fa flags
88argument affects the operational characteristics of
89.Fn malloc
90and
91.Fn mallocarray
92as follows:
93.Bl -tag -width xxx -offset indent
94.It Dv M_WAITOK
95If memory is currently unavailable,
96.Fn malloc
97may call sleep to wait for resources to be released by other processes.
98.It Dv M_NOWAIT
99Causes
100.Fn malloc
101to return
102.Dv NULL
103if the request cannot be immediately fulfilled due to resource shortage.
104.It Dv M_CANFAIL
105In the
106.Dv M_WAITOK
107case, if not enough memory is available, return
108.Dv NULL
109instead of calling
110.Xr panic 9 .
111If
112.Fn mallocarray
113detects an overflow
114or
115.Fn malloc
116detects an excessive allocation, return
117.Dv NULL
118instead of calling
119.Xr panic 9 .
120.It Dv M_ZERO
121Causes allocated memory to be zeroed.
122.El
123.Pp
124One of
125.Dv M_NOWAIT
126or
127.Dv M_WAITOK
128must be specified via the
129.Fa flags
130argument.
131.Pp
132The
133.Fa type
134argument broadly identifies the kernel subsystem for which the allocated
135memory was needed, and is commonly used to maintain statistics about
136kernel memory usage.
137These statistics can be examined using
138.Xr vmstat 8
139or
140.Xr systat 1
141if either of the kernel
142.Xr options 4
143.Cm KMEMSTATS
144or
145.Cm DEBUG
146are enabled.
147.Pp
148The following types are currently defined:
149.Pp
150.Bl -tag -offset indent -width XXXXXXXXXXXXXX -compact
151.It Dv M_FREE
152Should be on free list.
153.It Dv M_DEVBUF
154Device driver memory.
155.It Dv M_PCB
156Protocol control blocks.
157.It Dv M_RTABLE
158Routing tables.
159.It Dv M_IFADDR
160Interface addresses.
161.It Dv M_SYSCTL
162Sysctl persistent buffers.
163.It Dv M_COUNTERS
164Per-CPU Counters for use via
165.Xr counters_alloc 9 .
166.It Dv M_IOCTLOPS
167Ioctl data buffers.
168.It Dv M_IOV
169Large IOVs.
170.It Dv M_MOUNT
171VFS mount structs.
172.It Dv M_NFSREQ
173NFS request headers.
174.It Dv M_NFSMNT
175NFS mount structures.
176.It Dv M_VNODE
177Dynamically allocated vnodes.
178.It Dv M_DQUOT
179UFS quota entries.
180.It Dv M_UFSMNT
181UFS mount structures.
182.It Dv M_SHM
183SVID compatible shared memory segments.
184.It Dv M_VMMAP
185VM map structures.
186.It Dv M_SEM
187SVID compatible semaphores.
188.It Dv M_DIRHASH
189UFS directory hash structures.
190.It Dv M_ACPI
191ACPI structures.
192.It Dv M_VMPMAP
193VM pmap data.
194.It Dv M_FILEDESC
195Open file descriptor tables.
196.It Dv M_SIGIO
197Sigio structures.
198.It Dv M_PROC
199Proc structures.
200.It Dv M_SUBPROC
201Proc sub-structures.
202.It Dv M_MFSNODE
203MFS vnode private part.
204.It Dv M_NETADDR
205Export host address structures.
206.It Dv M_NFSSVC
207NFS server structures.
208.It Dv M_NFSD
209NFS server daemon structures.
210.It Dv M_IPMOPTS
211Internet multicast options.
212.It Dv M_IPMADDR
213Internet multicast addresses.
214.It Dv M_IFMADDR
215Link-level multicast addresses.
216.It Dv M_MRTABLE
217Multicast routing tables.
218.It Dv M_ISOFSMNT
219ISOFS mount structures.
220.It Dv M_ISOFSNODE
221ISOFS vnode private part.
222.It Dv M_MSDOSFSMNT
223MSDOS FS mount structures.
224.It Dv M_MSDOSFSFAT
225MSDOS FS FAT tables.
226.It Dv M_MSDOSFSNODE
227MSDOS FS vnode private part.
228.It Dv M_TTYS
229Allocated tty structures.
230.It Dv M_EXEC
231Argument lists & other mem used by exec.
232.It Dv M_MISCFSMNT
233Miscellaneous FS mount structures.
234.It Dv M_FUSEFS
235FUSE FS mount structures.
236.It Dv M_PFKEY
237Pfkey data.
238.It Dv M_TDB
239Transforms database.
240.It Dv M_XDATA
241IPsec data.
242.It Dv M_PAGEDEP
243File page dependencies.
244.It Dv M_INODEDEP
245Inode dependencies.
246.It Dv M_NEWBLK
247New block allocation.
248.It Dv M_INDIRDEP
249Indirect block dependencies.
250.It Dv M_VMSWAP
251VM swap structures.
252.It Dv M_UVMAMAP
253UVM amap and related.
254.It Dv M_UVMAOBJ
255UVM aobj and related.
256.It Dv M_USB
257USB general.
258.It Dv M_USBDEV
259USB device driver.
260.It Dv M_USBHC
261USB host controller.
262.It Dv M_WITNESS
263.Xr witness 4
264memory.
265.It Dv M_MEMDESC
266Memory range.
267.It Dv M_CRYPTO_DATA
268.Xr crypto 9
269data buffers.
270.It Dv M_CREDENTIALS
271.Xr ipsec 4
272related credentials.
273.It Dv M_IP6OPT
274IPv6 options.
275.It Dv M_IP6NDP
276IPv6 neighbour discovery structures.
277.It Dv M_TEMP
278Miscellaneous temporary data buffers.
279.It Dv M_NTFSMNT
280NTFS mount structures.
281.It Dv M_NTFSNTNODE
282NTFS ntnode information.
283.It Dv M_NTFSFNODE
284NTFS fnode information.
285.It Dv M_NTFSDIR
286NTFS directory buffers.
287.It Dv M_NTFSNTHASH
288NTFS ntnode hash tables.
289.It Dv M_NTFSNTVATTR
290NTFS file attribute information.
291.It Dv M_NTFSRDATA
292NTFS resident data.
293.It Dv M_NTFSDECOMP
294NTFS decompression temporary storage.
295.It Dv M_NTFSRUN
296NTFS vrun storage.
297.It Dv M_KEVENT
298.Xr kqueue 2
299data structures.
300.It Dv M_SYNCACHE
301SYN cache hash array.
302.It Dv M_UDFMOUNT
303UDF mount structures.
304.It Dv M_UDFFENTRY
305UDF file entries.
306.It Dv M_UDFFID
307UDF file ID.
308.It Dv M_AGP
309AGP memory.
310.It Dv M_DRM
311Direct Rendering Manager.
312.El
313.Sh CONTEXT
314.Fn malloc
315and
316.Fn mallocarray
317can be called during autoconf, from process context, or from interrupt context
318if
319.Dv M_NOWAIT
320is passed via
321.Fa flags .
322They can't be called from interrupt context if
323.Dv M_WAITOK
324is passed via
325.Fa flags .
326.Pp
327.Fn free
328can be called during autoconf, from process context, or from interrupt context.
329.Sh RETURN VALUES
330.Fn malloc
331and
332.Fn mallocarray
333return a kernel virtual address that is suitably aligned for storage of
334any type of object.
335.Sh DIAGNOSTICS
336A kernel compiled with the
337.Dv DIAGNOSTIC
338configuration option attempts to detect memory corruption caused by
339such things as writing outside the allocated area and unbalanced calls to
340.Fn malloc
341or
342.Fn mallocarray ,
343and
344.Fn free .
345Failing consistency checks will cause a panic or a system console message:
346.Pp
347.Bl -bullet -offset indent -compact
348.It
349panic:
350.Dq malloc: bogus type
351.It
352panic:
353.Dq malloc: out of space in kmem_map
354.It
355panic:
356.Dq malloc: allocation too large
357.It
358panic:
359.Dq malloc: wrong bucket
360.It
361panic:
362.Dq malloc: lost data
363.It
364panic:
365.Dq mallocarray: overflow
366.It
367panic:
368.Dq free: unaligned addr
369.It
370panic:
371.Dq free: duplicated free
372.It
373panic:
374.Dq free: multiple frees
375.It
376panic:
377.Dq free: non-malloced addr
378.It
379panic:
380.Dq free: size too large
381.It
382panic:
383.Dq free: size too small
384.It
385panic:
386.Dq kmeminit: minbucket too small/struct freelist too big
387.It
388.Dq multiply freed item Aq addr
389.It
390.Dq Data modified on freelist: Aq data object description
391.El
392.Sh SEE ALSO
393.Xr systat 1 ,
394.Xr vmstat 8
395