1 .\" $NetBSD: kmem.9,v 1.2 2009/08/03 20:10:16 rmind Exp $
3 .\" Copyright (c)2006 YAMAMOTO Takashi,
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 .\" ------------------------------------------------------------
31 .\" ------------------------------------------------------------
34 .Nd kernel wired memory allocator
35 .\" ------------------------------------------------------------
38 .\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41 "size_t size" "km_flag_t kmflags"
44 "size_t size" "km_flag_t kmflags"
47 "void *p" "size_t size"
48 .\" ------------------------------------------------------------
53 allocates kernel wired memory.
54 It takes the following arguments.
55 .Bl -tag -width kmflags
57 Specify the size of allocation in bytes.
59 Either of the following:
60 .Bl -tag -width KM_NOSLEEP
62 If the allocation cannot be satisfied immediately, sleep until enough
68 if there is not enough memory available.
69 It should only be used when failure to allocate will not have harmful,
75 is strongly discouraged as it can create transient, hard to debug failures
76 that occur when the system is under memory pressure.
79 In situations where it is not possible to sleep, for example because locks
80 are held by the caller, the code path should be restructured to allow the
81 allocation to be made in another place.
85 The contents of allocated memory are uninitialized.
87 Unlike Solaris, kmem_alloc(0, flags) is illegal.
89 .\" ------------------------------------------------------------
93 except that it initializes the memory to zero.
95 .\" ------------------------------------------------------------
97 frees kernel wired memory allocated by
101 so that it can be used for other purposes.
102 It takes the following arguments.
103 .Bl -tag -width kmflags
105 The pointer to the memory being freed.
106 It must be the one returned by
111 The size of the memory being freed, in bytes.
112 It must be the same as the
118 when the memory was allocated.
124 .\" ------------------------------------------------------------
128 allocations while holding mutexes or reader/writer locks is discouraged, as the
129 caller can sleep for an unbounded amount of time in order to satisfy the
131 This can in turn block other threads that wish to acquire locks held by the
134 For some locks this is permissible or even unavoidable.
135 For others, particularly locks that may be taken from soft interrupt context,
136 it is a serious problem.
137 As a general rule it is better not to allow this type of situation to develop.
138 One way to circumvent the problem is to make allocations speculative and part
139 of a retryable sequence.
143 /* speculative unlocked check */
144 if (need to allocate) {
145 new_item = kmem_alloc(sizeof(*new_item), KM_SLEEP);
150 /* check while holding lock for true status */
151 if (need to allocate) {
152 if (new_item == NULL) {
160 if (new_item != NULL) {
161 /* did not use it after all */
162 kmem_free(new_item, sizeof(*new_item));
165 .\" ------------------------------------------------------------
167 Kernels compiled with the
169 option perform CPU intensive sanity checks on kmem operations,
172 facility which can be enabled at runtime.
175 adds additional, very high overhead runtime verification to kmem operations.
176 To enable it, boot the system with the
178 option, which causes the debugger to be entered early during the kernel
180 Issue commands such as the following:
182 db\*[Gt] w kmem_guard_depth 0t30000
188 to queue up to 60000 (30000*2) pages of unmapped KVA to catch
189 use-after-free type errors.
192 is called, memory backing a freed item is unmapped and the kernel VA
193 space pushed onto a FIFO.
194 The VA space will not be reused until another 30k items have been freed.
195 Until reused the kernel will catch invalid accesses and panic with a page fault.
199 It has a severe impact on performance.
201 It is best used on a 64-bit machine with lots of RAM.
203 Allocations larger than PAGE_SIZE bypass the
208 kmguard tries to catch the following types of bugs:
211 Overflow at time of occurrence, by means of a guard page.
215 by using a canary value.
217 Invalid pointer or size passed, at
225 return a pointer to allocated memory.
229 .\" ------------------------------------------------------------
231 This section describes places within the
233 source tree where actual code implementing the
237 All pathnames are relative to
242 subsystem is implemented within the file
243 .Pa sys/kern/subr_kmem.c .
244 .\" ------------------------------------------------------------
247 .Xr memoryallocators 9 ,
249 .\" ------------------------------------------------------------
252 cannot be used from interrupt context, from a soft interrupt, or from
257 .\" ------------------------------------------------------------
258 .Sh SECURITY CONSIDERATION
259 As the memory allocated by
261 is uninitialized, it can contain security-sensitive data left by its
263 It is the caller's responsibility not to expose it to the world.