xref: /dflybsd-src/share/man/man9/kmalloc.9 (revision d11a3c07ced1c71d6103e7a190d9b4dd8eb16011)
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.
143Before the internal pool limit is reached,
144the
145.Fn kmalloc
146and
147.Fn krealloc ,
148functions cannot return
149.Dv NULL
150if
151.Dv M_WAITOK
152is specified.
153If the internal pool limit is reached and
154.Dv M_NULLOK
155is not specified along with
156.Dv M_WAITOK ,
157the system will panic.
158If the internal pool limit is reached and
159.Dv M_NULLOK
160is specified along with
161.Dv M_WAITOK ,
162the
163.Fn kmalloc
164and
165.Fn krealloc ,
166functions return
167.Dv NULL
168instead of panicing the system.
169.It Dv M_INTWAIT
170Indicates
171.Fn kmalloc
172to dig into the system's reserved free pages looking for enough room to
173perform the allocation.
174This is typically used in interrupts where you cannot afford
175.Fn kmalloc
176to fail.
177Before the internal pool limit is reached,
178the
179.Fn kmalloc
180and
181.Fn krealloc ,
182functions cannot return
183.Dv NULL
184if
185.Dv M_INTWAIT
186is specified.
187If the internal pool limit is reached and
188.Dv M_NULLOK
189is not specified along with
190.Dv M_INTWAIT ,
191the system will panic.
192If the internal pool limit is reached and
193.Dv M_NULLOK
194is specified along with
195.Dv M_INTWAIT ,
196the
197.Fn kmalloc
198and
199.Fn krealloc ,
200functions return
201.Dv NULL
202instead of panicing the system.
203.It Dv M_USE_RESERVE
204Indicates that the system can dig into its reserve in order to obtain the
205requested memory.
206This option used to be called
207.Dv M_KERNEL
208but has been renamed to something more obvious.
209This option has been deprecated and is slowly being removed from the kernel,
210and so should not be used with any new code.
211.It Dv M_POWEROF2
212Rounds up the size to the nearest power of 2.
213.It Dv M_NULLOK
214This flag is usually specified along with
215.Dv M_WAITOK
216or
217.Dv M_INTWAIT ,
218so when the interal pool limit is reached,
219.Fn kmalloc
220and
221.Fn krealloc ,
222functions will not panic the system,
223instead,
224.Dv NULL
225will be returned.
226This flag is usually used on the kernel code path that is triggered by
227user space programs' requests.
228.El
229.Pp
230Exactly one of either
231.Dv M_WAITOK
232or
233.Dv M_NOWAIT
234must be specified.
235.Pp
236The
237.Fa type
238argument is used to perform statistics on memory usage, and for
239basic sanity checks.
240It can be used to identify multiple allocations.
241The statistics can be examined by
242.Sq vmstat -m .
243.Pp
244A
245.Fa type
246is defined using the
247.Va malloc_type_t
248typedef via the
249.Fn MALLOC_DECLARE
250and
251.Fn MALLOC_DEFINE
252macros.
253.Bd -literal -offset indent
254/* sys/something/foo_extern.h */
255
256MALLOC_DECLARE(M_FOOBUF);
257
258/* sys/something/foo_main.c */
259
260MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
261
262/* sys/something/foo_subr.c */
263
264\&...
265buf = kmalloc(sizeof *buf, M_FOOBUF, M_NOWAIT);
266
267.Ed
268.Sh IMPLEMENTATION NOTES
269The memory allocator allocates memory in chunks that have size a power
270of two for requests up to the size of a page of memory.
271For larger requests, one or more pages is allocated.
272The allocated memory will be at least 8 bytes aligned.
273While it should not be relied upon, this information may be useful for
274optimizing the efficiency of memory use.
275.Sh RETURN VALUES
276The
277.Fn kmalloc
278and
279.Fn krealloc ,
280functions return a kernel virtual address that is suitably aligned for
281storage of any type of object, or
282.Dv NULL
283if the request could not be satisfied (implying that
284.Dv M_NOWAIT
285was set).
286.Sh DIAGNOSTICS
287A kernel compiled with the
288.Dv INVARIANTS
289configuration option attempts to detect memory corruption caused by
290such things as writing outside the allocated area and imbalanced calls to the
291.Fn kmalloc
292and
293.Fn kfree
294functions.
295Failing consistency checks will cause a panic or a system console
296message.
297.Sh SEE ALSO
298.Xr vmstat 8 ,
299.Xr contigmalloc 9 ,
300.Xr memory 9 ,
301.Xr vnode 9
302