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 September 22, 2013 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.Ft void 61.Fn kmalloc_raise_limit "struct malloc_type *type" "size_t bytes" 62.Fn MALLOC_DECLARE type 63.In sys/param.h 64.In sys/malloc.h 65.In sys/kernel.h 66.Fn MALLOC_DEFINE type shortdesc longdesc 67.Sh DESCRIPTION 68The 69.Fn kmalloc 70function allocates uninitialized memory in kernel address space for an 71object whose size is specified by 72.Fa size . 73.Fn kmalloc_cachealign 74function is same as 75.Fn kmalloc 76except that the allocated memory will be cache line size aligned. 77.Pp 78The 79.Fn kfree 80function releases memory at address 81.Fa addr 82that was previously allocated by 83.Fn kmalloc 84for re-use. 85The memory is not zeroed. 86The kernel implementation of 87.Fn kfree 88does not allow 89.Fa addr 90to be 91.Dv NULL . 92.Pp 93The 94.Fn krealloc 95function changes the size of the previously allocated memory referenced by 96.Fa addr 97to 98.Fa size 99bytes. 100The contents of the memory are unchanged up to the lesser of the new and 101old sizes. 102Note that the returned value may differ from 103.Fa addr . 104If the requested memory cannot be allocated, 105.Dv NULL 106is returned and the memory referenced by 107.Fa addr 108is valid and unchanged. 109If 110.Fa addr 111is 112.Dv NULL , 113the 114.Fn krealloc 115function behaves identically to 116.Fn kmalloc 117for the specified size. 118.Pp 119.Fn kmalloc_raise_limit 120is used to increase the internal pool limit to 121.Fa bytes . 122Under most of the cases 123the default internal pool limit should be more than enough, 124so this function is currently rarely used and must be used with care. 125.Pp 126Unlike its standard C library counterpart 127.Pq Xr malloc 3 , 128the kernel version takes two more arguments. 129The 130.Fa flags 131argument further qualifies 132.Fn kmalloc Ns 's 133operational characteristics as follows: 134.Bl -tag -width indent 135.It Dv M_ZERO 136Causes the allocated memory to be set to all zeros. 137.It Dv M_NOWAIT 138Causes 139.Fn kmalloc 140and 141.Fn krealloc , 142to return 143.Dv NULL 144if the request cannot be immediately fulfilled due to resource shortage. 145Note that 146.Dv M_NOWAIT 147is required when running in an interrupt context. 148.It Dv M_WAITOK 149Indicates that it is OK to wait for resources. 150If the request cannot be immediately fulfilled, the current process is put 151to sleep to wait for resources to be released by other processes. 152Before the internal pool limit is reached, 153the 154.Fn kmalloc 155and 156.Fn krealloc , 157functions cannot return 158.Dv NULL 159if 160.Dv M_WAITOK 161is specified. 162If the internal pool limit is reached and 163.Dv M_NULLOK 164is not specified along with 165.Dv M_WAITOK , 166the system will panic. 167If the internal pool limit is reached and 168.Dv M_NULLOK 169is specified along with 170.Dv M_WAITOK , 171the 172.Fn kmalloc 173and 174.Fn krealloc , 175functions return 176.Dv NULL 177instead of panicing the system. 178.It Dv M_INTWAIT 179Indicates 180.Fn kmalloc 181to dig into the system's reserved free pages looking for enough room to 182perform the allocation. 183This is typically used in interrupts where you cannot afford 184.Fn kmalloc 185to fail. 186Before the internal pool limit is reached, 187the 188.Fn kmalloc 189and 190.Fn krealloc , 191functions cannot return 192.Dv NULL 193if 194.Dv M_INTWAIT 195is specified. 196If the internal pool limit is reached and 197.Dv M_NULLOK 198is not specified along with 199.Dv M_INTWAIT , 200the system will panic. 201If the internal pool limit is reached and 202.Dv M_NULLOK 203is specified along with 204.Dv M_INTWAIT , 205the 206.Fn kmalloc 207and 208.Fn krealloc , 209functions return 210.Dv NULL 211instead of panicing the system. 212.It Dv M_USE_RESERVE 213Indicates that the system can dig into its reserve in order to obtain the 214requested memory. 215This option used to be called 216.Dv M_KERNEL 217but has been renamed to something more obvious. 218This option has been deprecated and is slowly being removed from the kernel, 219and so should not be used with any new code. 220.It Dv M_POWEROF2 221Rounds up the size to the nearest power of 2. 222.It Dv M_NULLOK 223This flag is usually specified along with 224.Dv M_WAITOK 225or 226.Dv M_INTWAIT , 227so when the interal pool limit is reached, 228.Fn kmalloc 229and 230.Fn krealloc , 231functions will not panic the system, 232instead, 233.Dv NULL 234will be returned. 235This flag is usually used on the kernel code path that is triggered by 236user space programs' requests. 237.El 238.Pp 239Exactly one of either 240.Dv M_WAITOK , 241.Dv M_INTWAIT 242or 243.Dv M_NOWAIT 244must be specified. 245.Pp 246The 247.Fa type 248argument is used to perform statistics on memory usage, and for 249basic sanity checks. 250It can be used to identify multiple allocations. 251The statistics can be examined by 252.Sq vmstat -m . 253.Pp 254A 255.Fa type 256is defined using the 257.Va malloc_type_t 258typedef via the 259.Fn MALLOC_DECLARE 260and 261.Fn MALLOC_DEFINE 262macros. 263.Bd -literal -offset indent 264/* sys/something/foo_extern.h */ 265 266MALLOC_DECLARE(M_FOOBUF); 267 268/* sys/something/foo_main.c */ 269 270MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether"); 271 272/* sys/something/foo_subr.c */ 273 274\&... 275buf = kmalloc(sizeof *buf, M_FOOBUF, M_NOWAIT); 276 277.Ed 278.Sh IMPLEMENTATION NOTES 279The memory allocator allocates memory in chunks that have size a power 280of two for requests up to the size of a page of memory. 281For larger requests, one or more pages is allocated. 282The allocated memory will be at least 8 bytes aligned. 283While it should not be relied upon, this information may be useful for 284optimizing the efficiency of memory use. 285.Sh RETURN VALUES 286The 287.Fn kmalloc 288and 289.Fn krealloc , 290functions return a kernel virtual address that is suitably aligned for 291storage of any type of object, or 292.Dv NULL 293if the request could not be satisfied (implying that 294.Dv M_NOWAIT 295was set). 296.Sh DIAGNOSTICS 297A kernel compiled with the 298.Dv INVARIANTS 299configuration option attempts to detect memory corruption caused by 300such things as writing outside the allocated area and imbalanced calls to the 301.Fn kmalloc 302and 303.Fn kfree 304functions. 305Failing consistency checks will cause a panic or a system console 306message. 307.Sh SEE ALSO 308.Xr vmstat 8 , 309.Xr contigmalloc 9 , 310.Xr memory 9 , 311.Xr vnode 9 312