.\" .\" Copyright (c) 1996 The NetBSD Foundation, Inc. .\" All rights reserved. .\" .\" This code is derived from software contributed to The NetBSD Foundation .\" by Paul Kranenburg. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the NetBSD .\" Foundation, Inc. and its contributors. .\" 4. Neither the name of The NetBSD Foundation nor the names of its .\" contributors may be used to endorse or promote products derived .\" from this software without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" .\" $NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $ .\" $FreeBSD: src/share/man/man9/malloc.9,v 1.42 2005/02/22 17:20:20 brueffer Exp $ .\" .Dd September 22, 2013 .Dt KMALLOC 9 .Os .Sh NAME .Nm kmalloc , .Nm kmalloc_cachealign , .Nm kfree , .Nm krealloc , .Nm kmalloc_raise_limit , .Nm MALLOC_DEFINE , .Nm MALLOC_DECLARE .Nd kernel memory management routines .Sh SYNOPSIS .In sys/types.h .In sys/malloc.h .Ft void * .Fn kmalloc "unsigned long size" "struct malloc_type *type" "int flags" .Ft void * .Fn kmalloc_cachealign "unsigned long size" "struct malloc_type *type" "int flags" .Ft void .Fn kfree "void *addr" "struct malloc_type *type" .Ft void * .Fn krealloc "void *addr" "unsigned long size" "struct malloc_type *type" "int flags" .Ft void .Fn kmalloc_raise_limit "struct malloc_type *type" "size_t bytes" .Fn MALLOC_DECLARE type .In sys/param.h .In sys/malloc.h .In sys/kernel.h .Fn MALLOC_DEFINE type shortdesc longdesc .Sh DESCRIPTION The .Fn kmalloc function allocates uninitialized memory in kernel address space for an object whose size is specified by .Fa size . .Fn kmalloc_cachealign function is same as .Fn kmalloc except that the allocated memory will be cache line size aligned. .Pp The .Fn kfree function releases memory at address .Fa addr that was previously allocated by .Fn kmalloc for re-use. The memory is not zeroed. The kernel implementation of .Fn kfree does not allow .Fa addr to be .Dv NULL . .Pp The .Fn krealloc function changes the size of the previously allocated memory referenced by .Fa addr to .Fa size bytes. The contents of the memory are unchanged up to the lesser of the new and old sizes. Note that the returned value may differ from .Fa addr . If the requested memory cannot be allocated, .Dv NULL is returned and the memory referenced by .Fa addr is valid and unchanged. If .Fa addr is .Dv NULL , the .Fn krealloc function behaves identically to .Fn kmalloc for the specified size. .Pp .Fn kmalloc_raise_limit is used to increase the internal pool limit to .Fa bytes . Under most of the cases the default internal pool limit should be more than enough, so this function is currently rarely used and must be used with care. .Pp Unlike its standard C library counterpart .Pq Xr malloc 3 , the kernel version takes two more arguments. The .Fa flags argument further qualifies .Fn kmalloc Ns 's operational characteristics as follows: .Bl -tag -width indent .It Dv M_ZERO Causes the allocated memory to be set to all zeros. .It Dv M_NOWAIT Causes .Fn kmalloc and .Fn krealloc , to return .Dv NULL if the request cannot be immediately fulfilled due to resource shortage. Note that .Dv M_NOWAIT is required when running in an interrupt context. .It Dv M_WAITOK Indicates that it is OK to wait for resources. If the request cannot be immediately fulfilled, the current process is put to sleep to wait for resources to be released by other processes. Before the internal pool limit is reached, the .Fn kmalloc and .Fn krealloc , functions cannot return .Dv NULL if .Dv M_WAITOK is specified. If the internal pool limit is reached and .Dv M_NULLOK is not specified along with .Dv M_WAITOK , the system will panic. If the internal pool limit is reached and .Dv M_NULLOK is specified along with .Dv M_WAITOK , the .Fn kmalloc and .Fn krealloc , functions return .Dv NULL instead of panicing the system. .It Dv M_INTWAIT Indicates .Fn kmalloc to dig into the system's reserved free pages looking for enough room to perform the allocation. This is typically used in interrupts where you cannot afford .Fn kmalloc to fail. Before the internal pool limit is reached, the .Fn kmalloc and .Fn krealloc , functions cannot return .Dv NULL if .Dv M_INTWAIT is specified. If the internal pool limit is reached and .Dv M_NULLOK is not specified along with .Dv M_INTWAIT , the system will panic. If the internal pool limit is reached and .Dv M_NULLOK is specified along with .Dv M_INTWAIT , the .Fn kmalloc and .Fn krealloc , functions return .Dv NULL instead of panicing the system. .It Dv M_USE_RESERVE Indicates that the system can dig into its reserve in order to obtain the requested memory. This option used to be called .Dv M_KERNEL but has been renamed to something more obvious. This option has been deprecated and is slowly being removed from the kernel, and so should not be used with any new code. .It Dv M_POWEROF2 Rounds up the size to the nearest power of 2. .It Dv M_NULLOK This flag is usually specified along with .Dv M_WAITOK or .Dv M_INTWAIT , so when the interal pool limit is reached, .Fn kmalloc and .Fn krealloc , functions will not panic the system, instead, .Dv NULL will be returned. This flag is usually used on the kernel code path that is triggered by user space programs' requests. .El .Pp Exactly one of either .Dv M_WAITOK , .Dv M_INTWAIT or .Dv M_NOWAIT must be specified. .Pp The .Fa type argument is used to perform statistics on memory usage, and for basic sanity checks. It can be used to identify multiple allocations. The statistics can be examined by .Sq vmstat -m . .Pp A .Fa type is defined using the .Va malloc_type_t typedef via the .Fn MALLOC_DECLARE and .Fn MALLOC_DEFINE macros. .Bd -literal -offset indent /* sys/something/foo_extern.h */ MALLOC_DECLARE(M_FOOBUF); /* sys/something/foo_main.c */ MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether"); /* sys/something/foo_subr.c */ \&... buf = kmalloc(sizeof *buf, M_FOOBUF, M_NOWAIT); .Ed .Sh IMPLEMENTATION NOTES The memory allocator allocates memory in chunks that have size a power of two for requests up to the size of a page of memory. For larger requests, one or more pages is allocated. The allocated memory will be at least 8 bytes aligned. While it should not be relied upon, this information may be useful for optimizing the efficiency of memory use. .Sh RETURN VALUES The .Fn kmalloc and .Fn krealloc , functions return a kernel virtual address that is suitably aligned for storage of any type of object, or .Dv NULL if the request could not be satisfied (implying that .Dv M_NOWAIT or .Dv M_NULLOK was set). .Sh DIAGNOSTICS A kernel compiled with the .Dv INVARIANTS configuration option attempts to detect memory corruption caused by such things as writing outside the allocated area and imbalanced calls to the .Fn kmalloc and .Fn kfree functions. Failing consistency checks will cause a panic or a system console message. .Sh SEE ALSO .Xr vmstat 8 , .Xr contigmalloc 9 , .Xr memory 9 , .Xr vnode 9