xref: /dflybsd-src/share/man/man9/kmalloc.9 (revision 6b4e1f6de204f18988c3cf51ab163d3d18ce7c9b)
1.\"
2.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
3.\" All rights reserved.
4.\"
5.\" This code is derived from software contributed to The NetBSD Foundation
6.\" by Paul Kranenburg.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"        This product includes software developed by the NetBSD
19.\"        Foundation, Inc. and its contributors.
20.\" 4. Neither the name of The NetBSD Foundation nor the names of its
21.\"    contributors may be used to endorse or promote products derived
22.\"    from this software without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34.\" POSSIBILITY OF SUCH DAMAGE.
35.\"
36.\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $
37.\" $FreeBSD: src/share/man/man9/malloc.9,v 1.42 2005/02/22 17:20:20 brueffer Exp $
38.\"
39.Dd October 8, 2012
40.Dt KMALLOC 9
41.Os
42.Sh NAME
43.Nm kmalloc ,
44.Nm kfree ,
45.Nm krealloc ,
46.Nm MALLOC_DEFINE ,
47.Nm MALLOC_DECLARE
48.Nd kernel memory management routines
49.Sh SYNOPSIS
50.In sys/types.h
51.In sys/malloc.h
52.Ft void *
53.Fn kmalloc "unsigned long size" "struct malloc_type *type" "int flags"
54.Ft void *
55.Fn kmalloc_cachealign "unsigned long size" "struct malloc_type *type" "int flags"
56.Ft void
57.Fn kfree "void *addr" "struct malloc_type *type"
58.Ft void *
59.Fn krealloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags"
60.Fn MALLOC_DECLARE type
61.In sys/param.h
62.In sys/malloc.h
63.In sys/kernel.h
64.Fn MALLOC_DEFINE type shortdesc longdesc
65.Sh DESCRIPTION
66The
67.Fn kmalloc
68function allocates uninitialized memory in kernel address space for an
69object whose size is specified by
70.Fa size .
71.Fn kmalloc_cachealign
72function is same as
73.Fn kmalloc
74except that the allocated memory will be cache line size aligned.
75.Pp
76The
77.Fn kfree
78function releases memory at address
79.Fa addr
80that was previously allocated by
81.Fn kmalloc
82for re-use.
83The memory is not zeroed.
84The kernel implementation of
85.Fn kfree
86does not allow
87.Fa addr
88to be
89.Dv NULL .
90.Pp
91The
92.Fn krealloc
93function changes the size of the previously allocated memory referenced by
94.Fa addr
95to
96.Fa size
97bytes.
98The contents of the memory are unchanged up to the lesser of the new and
99old sizes.
100Note that the returned value may differ from
101.Fa addr .
102If the requested memory cannot be allocated,
103.Dv NULL
104is returned and the memory referenced by
105.Fa addr
106is valid and unchanged.
107If
108.Fa addr
109is
110.Dv NULL ,
111the
112.Fn krealloc
113function behaves identically to
114.Fn kmalloc
115for the specified size.
116.Pp
117Unlike its standard C library counterpart
118.Pq Xr malloc 3 ,
119the kernel version takes two more arguments.
120The
121.Fa flags
122argument further qualifies
123.Fn kmalloc Ns 's
124operational characteristics as follows:
125.Bl -tag -width indent
126.It Dv M_ZERO
127Causes the allocated memory to be set to all zeros.
128.It Dv M_NOWAIT
129Causes
130.Fn kmalloc
131and
132.Fn krealloc ,
133to return
134.Dv NULL
135if the request cannot be immediately fulfilled due to resource shortage.
136Note that
137.Dv M_NOWAIT
138is required when running in an interrupt context.
139.It Dv M_WAITOK
140Indicates that it is OK to wait for resources.
141If the request cannot be immediately fulfilled, the current process is put
142to sleep to wait for resources to be released by other processes.
143The
144.Fn kmalloc
145and
146.Fn krealloc ,
147functions cannot return
148.Dv NULL
149if
150.Dv M_WAITOK
151is specified.
152.It Dv M_INTWAIT
153Indicates
154.Fn kmalloc
155to dig into the system's reserved free pages looking for enough room to
156perform the allocation.
157This is typically used in interrupts where you cannot afford
158.Fn kmalloc
159to fail.
160.It Dv M_USE_RESERVE
161Indicates that the system can dig into its reserve in order to obtain the
162requested memory.
163This option used to be called
164.Dv M_KERNEL
165but has been renamed to something more obvious.
166This option has been deprecated and is slowly being removed from the kernel,
167and so should not be used with any new code.
168.It Dv M_POWEROF2
169Rounds up the size to the nearest power of 2.
170.El
171.Pp
172Exactly one of either
173.Dv M_WAITOK
174or
175.Dv M_NOWAIT
176must be specified.
177.Pp
178The
179.Fa type
180argument is used to perform statistics on memory usage, and for
181basic sanity checks.
182It can be used to identify multiple allocations.
183The statistics can be examined by
184.Sq vmstat -m .
185.Pp
186A
187.Fa type
188is defined using the
189.Va malloc_type_t
190typedef via the
191.Fn MALLOC_DECLARE
192and
193.Fn MALLOC_DEFINE
194macros.
195.Bd -literal -offset indent
196/* sys/something/foo_extern.h */
197
198MALLOC_DECLARE(M_FOOBUF);
199
200/* sys/something/foo_main.c */
201
202MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
203
204/* sys/something/foo_subr.c */
205
206\&...
207buf = kmalloc(sizeof *buf, M_FOOBUF, M_NOWAIT);
208
209.Ed
210.Sh IMPLEMENTATION NOTES
211The memory allocator allocates memory in chunks that have size a power
212of two for requests up to the size of a page of memory.
213For larger requests, one or more pages is allocated.
214While it should not be relied upon, this information may be useful for
215optimizing the efficiency of memory use.
216.Sh RETURN VALUES
217The
218.Fn kmalloc
219and
220.Fn krealloc ,
221functions return a kernel virtual address that is suitably aligned for
222storage of any type of object, or
223.Dv NULL
224if the request could not be satisfied (implying that
225.Dv M_NOWAIT
226was set).
227.Sh DIAGNOSTICS
228A kernel compiled with the
229.Dv INVARIANTS
230configuration option attempts to detect memory corruption caused by
231such things as writing outside the allocated area and imbalanced calls to the
232.Fn kmalloc
233and
234.Fn kfree
235functions.
236Failing consistency checks will cause a panic or a system console
237message.
238.Sh SEE ALSO
239.Xr vmstat 8 ,
240.Xr contigmalloc 9 ,
241.Xr memory 9 ,
242.Xr vnode 9
243