subr_kmem.c revision 1.89 1 1.89 ad /* $NetBSD: subr_kmem.c,v 1.89 2023/09/10 14:29:13 ad Exp $ */
2 1.1 yamt
3 1.76 maxv /*
4 1.89 ad * Copyright (c) 2009-2023 The NetBSD Foundation, Inc.
5 1.23 ad * All rights reserved.
6 1.23 ad *
7 1.23 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.61 maxv * by Andrew Doran and Maxime Villard.
9 1.23 ad *
10 1.23 ad * Redistribution and use in source and binary forms, with or without
11 1.23 ad * modification, are permitted provided that the following conditions
12 1.23 ad * are met:
13 1.23 ad * 1. Redistributions of source code must retain the above copyright
14 1.23 ad * notice, this list of conditions and the following disclaimer.
15 1.23 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.23 ad * notice, this list of conditions and the following disclaimer in the
17 1.23 ad * documentation and/or other materials provided with the distribution.
18 1.23 ad *
19 1.23 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.23 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.23 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.23 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.23 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.23 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.23 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.23 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.23 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.23 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.23 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.23 ad */
31 1.23 ad
32 1.76 maxv /*
33 1.1 yamt * Copyright (c)2006 YAMAMOTO Takashi,
34 1.1 yamt * All rights reserved.
35 1.1 yamt *
36 1.1 yamt * Redistribution and use in source and binary forms, with or without
37 1.1 yamt * modification, are permitted provided that the following conditions
38 1.1 yamt * are met:
39 1.1 yamt * 1. Redistributions of source code must retain the above copyright
40 1.1 yamt * notice, this list of conditions and the following disclaimer.
41 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 yamt * notice, this list of conditions and the following disclaimer in the
43 1.1 yamt * documentation and/or other materials provided with the distribution.
44 1.1 yamt *
45 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 yamt * SUCH DAMAGE.
56 1.1 yamt */
57 1.1 yamt
58 1.1 yamt /*
59 1.55 maxv * Allocator of kernel wired memory. This allocator has some debug features
60 1.55 maxv * enabled with "option DIAGNOSTIC" and "option DEBUG".
61 1.50 yamt */
62 1.50 yamt
63 1.50 yamt /*
64 1.55 maxv * KMEM_SIZE: detect alloc/free size mismatch bugs.
65 1.79 ad * Append to each allocation a fixed-sized footer and record the exact
66 1.79 ad * user-requested allocation size in it. When freeing, compare it with
67 1.79 ad * kmem_free's "size" argument.
68 1.60 maxv *
69 1.76 maxv * This option is enabled on DIAGNOSTIC.
70 1.50 yamt *
71 1.79 ad * |CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK| |
72 1.79 ad * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-+
73 1.79 ad * | | | | | | | | |/////|U|
74 1.79 ad * | | | | | | | | |/HSZ/|U|
75 1.79 ad * | | | | | | | | |/////|U|
76 1.79 ad * +-----+-----+-----+-----+-----+-----+-----+-----+-----+-+
77 1.79 ad * | Buffer usable by the caller (requested size) |Size |Unused
78 1.60 maxv */
79 1.60 maxv
80 1.1 yamt #include <sys/cdefs.h>
81 1.89 ad __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.89 2023/09/10 14:29:13 ad Exp $");
82 1.63 christos
83 1.63 christos #ifdef _KERNEL_OPT
84 1.63 christos #include "opt_kmem.h"
85 1.63 christos #endif
86 1.1 yamt
87 1.1 yamt #include <sys/param.h>
88 1.6 yamt #include <sys/callback.h>
89 1.1 yamt #include <sys/kmem.h>
90 1.39 para #include <sys/pool.h>
91 1.13 ad #include <sys/debug.h>
92 1.17 ad #include <sys/lockdebug.h>
93 1.23 ad #include <sys/cpu.h>
94 1.69 maxv #include <sys/asan.h>
95 1.77 maxv #include <sys/msan.h>
96 1.85 riastrad #include <sys/sdt.h>
97 1.69 maxv
98 1.6 yamt #include <uvm/uvm_extern.h>
99 1.6 yamt #include <uvm/uvm_map.h>
100 1.6 yamt
101 1.1 yamt #include <lib/libkern/libkern.h>
102 1.1 yamt
103 1.46 para struct kmem_cache_info {
104 1.40 rmind size_t kc_size;
105 1.40 rmind const char * kc_name;
106 1.85 riastrad #ifdef KDTRACE_HOOKS
107 1.85 riastrad const id_t *kc_alloc_probe_id;
108 1.85 riastrad const id_t *kc_free_probe_id;
109 1.85 riastrad #endif
110 1.46 para };
111 1.46 para
112 1.85 riastrad #define KMEM_CACHE_SIZES(F) \
113 1.85 riastrad F(8, kmem-00008, kmem__00008) \
114 1.85 riastrad F(16, kmem-00016, kmem__00016) \
115 1.85 riastrad F(24, kmem-00024, kmem__00024) \
116 1.85 riastrad F(32, kmem-00032, kmem__00032) \
117 1.85 riastrad F(40, kmem-00040, kmem__00040) \
118 1.85 riastrad F(48, kmem-00048, kmem__00048) \
119 1.85 riastrad F(56, kmem-00056, kmem__00056) \
120 1.85 riastrad F(64, kmem-00064, kmem__00064) \
121 1.85 riastrad F(80, kmem-00080, kmem__00080) \
122 1.85 riastrad F(96, kmem-00096, kmem__00096) \
123 1.85 riastrad F(112, kmem-00112, kmem__00112) \
124 1.85 riastrad F(128, kmem-00128, kmem__00128) \
125 1.85 riastrad F(160, kmem-00160, kmem__00160) \
126 1.85 riastrad F(192, kmem-00192, kmem__00192) \
127 1.85 riastrad F(224, kmem-00224, kmem__00224) \
128 1.85 riastrad F(256, kmem-00256, kmem__00256) \
129 1.85 riastrad F(320, kmem-00320, kmem__00320) \
130 1.85 riastrad F(384, kmem-00384, kmem__00384) \
131 1.85 riastrad F(448, kmem-00448, kmem__00448) \
132 1.85 riastrad F(512, kmem-00512, kmem__00512) \
133 1.85 riastrad F(768, kmem-00768, kmem__00768) \
134 1.85 riastrad F(1024, kmem-01024, kmem__01024) \
135 1.85 riastrad /* end of KMEM_CACHE_SIZES */
136 1.85 riastrad
137 1.85 riastrad #define KMEM_CACHE_BIG_SIZES(F) \
138 1.85 riastrad F(2048, kmem-02048, kmem__02048) \
139 1.85 riastrad F(4096, kmem-04096, kmem__04096) \
140 1.85 riastrad F(8192, kmem-08192, kmem__08192) \
141 1.85 riastrad F(16384, kmem-16384, kmem__16384) \
142 1.85 riastrad /* end of KMEM_CACHE_BIG_SIZES */
143 1.85 riastrad
144 1.85 riastrad /* sdt:kmem:alloc:kmem-* probes */
145 1.85 riastrad #define F(SZ, NAME, PROBENAME) \
146 1.85 riastrad SDT_PROBE_DEFINE4(sdt, kmem, alloc, PROBENAME, \
147 1.85 riastrad "void *"/*ptr*/, \
148 1.85 riastrad "size_t"/*requested_size*/, \
149 1.85 riastrad "size_t"/*allocated_size*/, \
150 1.85 riastrad "km_flag_t"/*kmflags*/);
151 1.85 riastrad KMEM_CACHE_SIZES(F);
152 1.85 riastrad KMEM_CACHE_BIG_SIZES(F);
153 1.85 riastrad #undef F
154 1.85 riastrad
155 1.85 riastrad /* sdt:kmem:free:kmem-* probes */
156 1.85 riastrad #define F(SZ, NAME, PROBENAME) \
157 1.85 riastrad SDT_PROBE_DEFINE3(sdt, kmem, free, PROBENAME, \
158 1.85 riastrad "void *"/*ptr*/, \
159 1.85 riastrad "size_t"/*requested_size*/, \
160 1.85 riastrad "size_t"/*allocated_size*/);
161 1.85 riastrad KMEM_CACHE_SIZES(F);
162 1.85 riastrad KMEM_CACHE_BIG_SIZES(F);
163 1.85 riastrad #undef F
164 1.85 riastrad
165 1.85 riastrad /* sdt:kmem:alloc:large, sdt:kmem:free:large probes */
166 1.85 riastrad SDT_PROBE_DEFINE4(sdt, kmem, alloc, large,
167 1.85 riastrad "void *"/*ptr*/,
168 1.85 riastrad "size_t"/*requested_size*/,
169 1.85 riastrad "size_t"/*allocated_size*/,
170 1.85 riastrad "km_flag_t"/*kmflags*/);
171 1.85 riastrad SDT_PROBE_DEFINE3(sdt, kmem, free, large,
172 1.85 riastrad "void *"/*ptr*/,
173 1.85 riastrad "size_t"/*requested_size*/,
174 1.85 riastrad "size_t"/*allocated_size*/);
175 1.85 riastrad
176 1.85 riastrad #ifdef KDTRACE_HOOKS
177 1.85 riastrad #define F(SZ, NAME, PROBENAME) \
178 1.85 riastrad { SZ, #NAME, \
179 1.85 riastrad &sdt_sdt_kmem_alloc_##PROBENAME->id, \
180 1.85 riastrad &sdt_sdt_kmem_free_##PROBENAME->id },
181 1.85 riastrad #else
182 1.85 riastrad #define F(SZ, NAME, PROBENAME) { SZ, #NAME },
183 1.85 riastrad #endif
184 1.85 riastrad
185 1.46 para static const struct kmem_cache_info kmem_cache_sizes[] = {
186 1.85 riastrad KMEM_CACHE_SIZES(F)
187 1.87 mrg { 0 }
188 1.46 para };
189 1.46 para
190 1.46 para static const struct kmem_cache_info kmem_cache_big_sizes[] = {
191 1.85 riastrad KMEM_CACHE_BIG_SIZES(F)
192 1.87 mrg { 0 }
193 1.39 para };
194 1.1 yamt
195 1.85 riastrad #undef F
196 1.85 riastrad
197 1.39 para /*
198 1.40 rmind * KMEM_ALIGN is the smallest guaranteed alignment and also the
199 1.46 para * smallest allocateable quantum.
200 1.46 para * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
201 1.39 para */
202 1.40 rmind #define KMEM_ALIGN 8
203 1.40 rmind #define KMEM_SHIFT 3
204 1.46 para #define KMEM_MAXSIZE 1024
205 1.40 rmind #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
206 1.1 yamt
207 1.40 rmind static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
208 1.40 rmind static size_t kmem_cache_maxidx __read_mostly;
209 1.23 ad
210 1.46 para #define KMEM_BIG_ALIGN 2048
211 1.46 para #define KMEM_BIG_SHIFT 11
212 1.46 para #define KMEM_BIG_MAXSIZE 16384
213 1.46 para #define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
214 1.46 para
215 1.46 para static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
216 1.46 para static size_t kmem_cache_big_maxidx __read_mostly;
217 1.46 para
218 1.53 maxv #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
219 1.57 maxv #define KMEM_SIZE
220 1.67 maxv #endif
221 1.53 maxv
222 1.45 martin #if defined(DEBUG) && defined(_HARDKERNEL)
223 1.61 maxv static void *kmem_freecheck;
224 1.67 maxv #endif
225 1.4 yamt
226 1.23 ad #if defined(KMEM_SIZE)
227 1.79 ad #define SIZE_SIZE sizeof(size_t)
228 1.23 ad static void kmem_size_set(void *, size_t);
229 1.39 para static void kmem_size_check(void *, size_t);
230 1.23 ad #else
231 1.23 ad #define SIZE_SIZE 0
232 1.23 ad #define kmem_size_set(p, sz) /* nothing */
233 1.23 ad #define kmem_size_check(p, sz) /* nothing */
234 1.23 ad #endif
235 1.23 ad
236 1.85 riastrad #ifndef KDTRACE_HOOKS
237 1.85 riastrad
238 1.85 riastrad static const id_t **const kmem_cache_alloc_probe_id = NULL;
239 1.85 riastrad static const id_t **const kmem_cache_big_alloc_probe_id = NULL;
240 1.85 riastrad static const id_t **const kmem_cache_free_probe_id = NULL;
241 1.85 riastrad static const id_t **const kmem_cache_big_free_probe_id = NULL;
242 1.85 riastrad
243 1.85 riastrad #define KMEM_CACHE_PROBE(ARRAY, INDEX, PTR, REQSIZE, ALLOCSIZE, FLAGS) \
244 1.85 riastrad __nothing
245 1.85 riastrad
246 1.85 riastrad #else
247 1.85 riastrad
248 1.85 riastrad static const id_t *kmem_cache_alloc_probe_id[KMEM_CACHE_COUNT];
249 1.85 riastrad static const id_t *kmem_cache_big_alloc_probe_id[KMEM_CACHE_COUNT];
250 1.85 riastrad static const id_t *kmem_cache_free_probe_id[KMEM_CACHE_COUNT];
251 1.85 riastrad static const id_t *kmem_cache_big_free_probe_id[KMEM_CACHE_COUNT];
252 1.85 riastrad
253 1.85 riastrad #define KMEM_CACHE_PROBE(ARRAY, INDEX, PTR, REQSIZE, ALLOCSIZE, FLAGS) do \
254 1.85 riastrad { \
255 1.85 riastrad id_t id; \
256 1.85 riastrad \
257 1.85 riastrad KDASSERT((INDEX) < __arraycount(ARRAY)); \
258 1.85 riastrad if (__predict_false((id = *(ARRAY)[INDEX]) != 0)) { \
259 1.85 riastrad (*sdt_probe_func)(id, \
260 1.85 riastrad (uintptr_t)(PTR), \
261 1.85 riastrad (uintptr_t)(REQSIZE), \
262 1.85 riastrad (uintptr_t)(ALLOCSIZE), \
263 1.85 riastrad (uintptr_t)(FLAGS), \
264 1.85 riastrad (uintptr_t)0); \
265 1.85 riastrad } \
266 1.85 riastrad } while (0)
267 1.85 riastrad
268 1.85 riastrad #endif /* KDTRACE_HOOKS */
269 1.85 riastrad
270 1.85 riastrad #define KMEM_CACHE_ALLOC_PROBE(I, P, RS, AS, F) \
271 1.85 riastrad KMEM_CACHE_PROBE(kmem_cache_alloc_probe_id, I, P, RS, AS, F)
272 1.85 riastrad #define KMEM_CACHE_BIG_ALLOC_PROBE(I, P, RS, AS, F) \
273 1.85 riastrad KMEM_CACHE_PROBE(kmem_cache_big_alloc_probe_id, I, P, RS, AS, F)
274 1.85 riastrad #define KMEM_CACHE_FREE_PROBE(I, P, RS, AS) \
275 1.85 riastrad KMEM_CACHE_PROBE(kmem_cache_free_probe_id, I, P, RS, AS, 0)
276 1.85 riastrad #define KMEM_CACHE_BIG_FREE_PROBE(I, P, RS, AS) \
277 1.85 riastrad KMEM_CACHE_PROBE(kmem_cache_big_free_probe_id, I, P, RS, AS, 0)
278 1.85 riastrad
279 1.32 skrll CTASSERT(KM_SLEEP == PR_WAITOK);
280 1.32 skrll CTASSERT(KM_NOSLEEP == PR_NOWAIT);
281 1.32 skrll
282 1.46 para /*
283 1.46 para * kmem_intr_alloc: allocate wired memory.
284 1.46 para */
285 1.39 para void *
286 1.50 yamt kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
287 1.1 yamt {
288 1.71 christos #ifdef KASAN
289 1.70 maxv const size_t origsize = requested_size;
290 1.71 christos #endif
291 1.40 rmind size_t allocsz, index;
292 1.50 yamt size_t size;
293 1.39 para pool_cache_t pc;
294 1.39 para uint8_t *p;
295 1.1 yamt
296 1.50 yamt KASSERT(requested_size > 0);
297 1.1 yamt
298 1.65 riastrad KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
299 1.65 riastrad KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
300 1.65 riastrad
301 1.69 maxv kasan_add_redzone(&requested_size);
302 1.50 yamt size = kmem_roundup_size(requested_size);
303 1.54 maxv allocsz = size + SIZE_SIZE;
304 1.54 maxv
305 1.85 riastrad if ((index = ((allocsz - 1) >> KMEM_SHIFT))
306 1.46 para < kmem_cache_maxidx) {
307 1.46 para pc = kmem_cache[index];
308 1.85 riastrad p = pool_cache_get(pc, kmflags);
309 1.85 riastrad KMEM_CACHE_ALLOC_PROBE(index,
310 1.85 riastrad p, requested_size, allocsz, kmflags);
311 1.46 para } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
312 1.55 maxv < kmem_cache_big_maxidx) {
313 1.46 para pc = kmem_cache_big[index];
314 1.85 riastrad p = pool_cache_get(pc, kmflags);
315 1.85 riastrad KMEM_CACHE_BIG_ALLOC_PROBE(index,
316 1.85 riastrad p, requested_size, allocsz, kmflags);
317 1.48 uebayasi } else {
318 1.40 rmind int ret = uvm_km_kmem_alloc(kmem_va_arena,
319 1.43 para (vsize_t)round_page(size),
320 1.39 para ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
321 1.39 para | VM_INSTANTFIT, (vmem_addr_t *)&p);
322 1.85 riastrad SDT_PROBE4(sdt, kmem, alloc, large,
323 1.85 riastrad ret ? NULL : p, requested_size, round_page(size), kmflags);
324 1.46 para if (ret) {
325 1.46 para return NULL;
326 1.46 para }
327 1.46 para FREECHECK_OUT(&kmem_freecheck, p);
328 1.89 ad KASSERT(size < coherency_unit ||
329 1.89 ad ALIGNED_POINTER(p, coherency_unit));
330 1.46 para return p;
331 1.1 yamt }
332 1.1 yamt
333 1.39 para if (__predict_true(p != NULL)) {
334 1.39 para FREECHECK_OUT(&kmem_freecheck, p);
335 1.50 yamt kmem_size_set(p, requested_size);
336 1.75 maxv kasan_mark(p, origsize, size, KASAN_KMEM_REDZONE);
337 1.68 maxv return p;
338 1.39 para }
339 1.89 ad
340 1.89 ad KASSERT(size < coherency_unit || ALIGNED_POINTER(p, coherency_unit));
341 1.47 para return p;
342 1.1 yamt }
343 1.1 yamt
344 1.46 para /*
345 1.46 para * kmem_intr_zalloc: allocate zeroed wired memory.
346 1.46 para */
347 1.39 para void *
348 1.39 para kmem_intr_zalloc(size_t size, km_flag_t kmflags)
349 1.23 ad {
350 1.39 para void *p;
351 1.23 ad
352 1.39 para p = kmem_intr_alloc(size, kmflags);
353 1.88 riastrad if (__predict_true(p != NULL)) {
354 1.39 para memset(p, 0, size);
355 1.39 para }
356 1.39 para return p;
357 1.23 ad }
358 1.23 ad
359 1.46 para /*
360 1.46 para * kmem_intr_free: free wired memory allocated by kmem_alloc.
361 1.46 para */
362 1.39 para void
363 1.50 yamt kmem_intr_free(void *p, size_t requested_size)
364 1.23 ad {
365 1.40 rmind size_t allocsz, index;
366 1.50 yamt size_t size;
367 1.39 para pool_cache_t pc;
368 1.23 ad
369 1.39 para KASSERT(p != NULL);
370 1.84 riastrad KASSERTMSG(requested_size > 0, "kmem_intr_free(%p, 0)", p);
371 1.39 para
372 1.69 maxv kasan_add_redzone(&requested_size);
373 1.50 yamt size = kmem_roundup_size(requested_size);
374 1.54 maxv allocsz = size + SIZE_SIZE;
375 1.54 maxv
376 1.85 riastrad if ((index = ((allocsz - 1) >> KMEM_SHIFT))
377 1.46 para < kmem_cache_maxidx) {
378 1.85 riastrad KMEM_CACHE_FREE_PROBE(index, p, requested_size, allocsz);
379 1.46 para pc = kmem_cache[index];
380 1.46 para } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
381 1.55 maxv < kmem_cache_big_maxidx) {
382 1.85 riastrad KMEM_CACHE_BIG_FREE_PROBE(index, p, requested_size, allocsz);
383 1.46 para pc = kmem_cache_big[index];
384 1.46 para } else {
385 1.46 para FREECHECK_IN(&kmem_freecheck, p);
386 1.85 riastrad SDT_PROBE3(sdt, kmem, free, large,
387 1.85 riastrad p, requested_size, round_page(size));
388 1.39 para uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
389 1.43 para round_page(size));
390 1.39 para return;
391 1.39 para }
392 1.39 para
393 1.75 maxv kasan_mark(p, size, size, 0);
394 1.70 maxv
395 1.50 yamt kmem_size_check(p, requested_size);
396 1.39 para FREECHECK_IN(&kmem_freecheck, p);
397 1.46 para LOCKDEBUG_MEM_CHECK(p, size);
398 1.39 para
399 1.39 para pool_cache_put(pc, p);
400 1.23 ad }
401 1.23 ad
402 1.76 maxv /* -------------------------------- Kmem API -------------------------------- */
403 1.1 yamt
404 1.1 yamt /*
405 1.1 yamt * kmem_alloc: allocate wired memory.
406 1.1 yamt * => must not be called from interrupt context.
407 1.1 yamt */
408 1.1 yamt void *
409 1.1 yamt kmem_alloc(size_t size, km_flag_t kmflags)
410 1.1 yamt {
411 1.62 chs void *v;
412 1.62 chs
413 1.88 riastrad KASSERT(!cpu_intr_p());
414 1.88 riastrad KASSERT(!cpu_softintr_p());
415 1.88 riastrad
416 1.62 chs v = kmem_intr_alloc(size, kmflags);
417 1.77 maxv if (__predict_true(v != NULL)) {
418 1.77 maxv kmsan_mark(v, size, KMSAN_STATE_UNINIT);
419 1.77 maxv kmsan_orig(v, size, KMSAN_TYPE_KMEM, __RET_ADDR);
420 1.77 maxv }
421 1.62 chs KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
422 1.62 chs return v;
423 1.1 yamt }
424 1.1 yamt
425 1.1 yamt /*
426 1.39 para * kmem_zalloc: allocate zeroed wired memory.
427 1.2 yamt * => must not be called from interrupt context.
428 1.2 yamt */
429 1.2 yamt void *
430 1.2 yamt kmem_zalloc(size_t size, km_flag_t kmflags)
431 1.2 yamt {
432 1.62 chs void *v;
433 1.62 chs
434 1.88 riastrad KASSERT(!cpu_intr_p());
435 1.88 riastrad KASSERT(!cpu_softintr_p());
436 1.88 riastrad
437 1.62 chs v = kmem_intr_zalloc(size, kmflags);
438 1.62 chs KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
439 1.62 chs return v;
440 1.2 yamt }
441 1.2 yamt
442 1.2 yamt /*
443 1.1 yamt * kmem_free: free wired memory allocated by kmem_alloc.
444 1.1 yamt * => must not be called from interrupt context.
445 1.1 yamt */
446 1.1 yamt void
447 1.1 yamt kmem_free(void *p, size_t size)
448 1.1 yamt {
449 1.88 riastrad
450 1.23 ad KASSERT(!cpu_intr_p());
451 1.27 ad KASSERT(!cpu_softintr_p());
452 1.88 riastrad
453 1.39 para kmem_intr_free(p, size);
454 1.77 maxv kmsan_mark(p, size, KMSAN_STATE_INITED);
455 1.1 yamt }
456 1.1 yamt
457 1.46 para static size_t
458 1.39 para kmem_create_caches(const struct kmem_cache_info *array,
459 1.85 riastrad const id_t *alloc_probe_table[], const id_t *free_probe_table[],
460 1.46 para pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
461 1.1 yamt {
462 1.46 para size_t maxidx = 0;
463 1.46 para size_t table_unit = (1 << shift);
464 1.39 para size_t size = table_unit;
465 1.23 ad int i;
466 1.1 yamt
467 1.39 para for (i = 0; array[i].kc_size != 0 ; i++) {
468 1.40 rmind const char *name = array[i].kc_name;
469 1.39 para size_t cache_size = array[i].kc_size;
470 1.46 para struct pool_allocator *pa;
471 1.74 maxv int flags = 0;
472 1.40 rmind pool_cache_t pc;
473 1.39 para size_t align;
474 1.39 para
475 1.39 para /* check if we reached the requested size */
476 1.46 para if (cache_size > maxsize || cache_size > PAGE_SIZE) {
477 1.23 ad break;
478 1.40 rmind }
479 1.78 ad
480 1.78 ad /*
481 1.78 ad * Exclude caches with size not a factor or multiple of the
482 1.78 ad * coherency unit.
483 1.78 ad */
484 1.78 ad if (cache_size < COHERENCY_UNIT) {
485 1.78 ad if (COHERENCY_UNIT % cache_size > 0) {
486 1.78 ad continue;
487 1.78 ad }
488 1.78 ad flags |= PR_NOTOUCH;
489 1.78 ad align = KMEM_ALIGN;
490 1.78 ad } else if ((cache_size & (PAGE_SIZE - 1)) == 0) {
491 1.78 ad align = PAGE_SIZE;
492 1.78 ad } else {
493 1.78 ad if ((cache_size % COHERENCY_UNIT) > 0) {
494 1.78 ad continue;
495 1.78 ad }
496 1.78 ad align = COHERENCY_UNIT;
497 1.46 para }
498 1.46 para
499 1.46 para if ((cache_size >> shift) > maxidx) {
500 1.46 para maxidx = cache_size >> shift;
501 1.40 rmind }
502 1.1 yamt
503 1.46 para pa = &pool_allocator_kmem;
504 1.39 para pc = pool_cache_init(cache_size, align, 0, flags,
505 1.46 para name, pa, ipl, NULL, NULL, NULL);
506 1.1 yamt
507 1.39 para while (size <= cache_size) {
508 1.46 para alloc_table[(size - 1) >> shift] = pc;
509 1.86 mrg #ifdef KDTRACE_HOOKS
510 1.85 riastrad if (alloc_probe_table) {
511 1.85 riastrad alloc_probe_table[(size - 1) >> shift] =
512 1.85 riastrad array[i].kc_alloc_probe_id;
513 1.85 riastrad }
514 1.85 riastrad if (free_probe_table) {
515 1.85 riastrad free_probe_table[(size - 1) >> shift] =
516 1.85 riastrad array[i].kc_free_probe_id;
517 1.85 riastrad }
518 1.86 mrg #endif
519 1.39 para size += table_unit;
520 1.39 para }
521 1.1 yamt }
522 1.46 para return maxidx;
523 1.1 yamt }
524 1.1 yamt
525 1.39 para void
526 1.39 para kmem_init(void)
527 1.1 yamt {
528 1.46 para kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
529 1.85 riastrad kmem_cache_alloc_probe_id, kmem_cache_free_probe_id,
530 1.46 para kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
531 1.55 maxv kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
532 1.85 riastrad kmem_cache_big_alloc_probe_id, kmem_cache_big_free_probe_id,
533 1.46 para kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
534 1.1 yamt }
535 1.4 yamt
536 1.39 para size_t
537 1.39 para kmem_roundup_size(size_t size)
538 1.7 yamt {
539 1.61 maxv return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
540 1.61 maxv }
541 1.7 yamt
542 1.61 maxv /*
543 1.61 maxv * Used to dynamically allocate string with kmem accordingly to format.
544 1.61 maxv */
545 1.61 maxv char *
546 1.61 maxv kmem_asprintf(const char *fmt, ...)
547 1.61 maxv {
548 1.61 maxv int size __diagused, len;
549 1.61 maxv va_list va;
550 1.61 maxv char *str;
551 1.61 maxv
552 1.61 maxv va_start(va, fmt);
553 1.61 maxv len = vsnprintf(NULL, 0, fmt, va);
554 1.61 maxv va_end(va);
555 1.61 maxv
556 1.61 maxv str = kmem_alloc(len + 1, KM_SLEEP);
557 1.61 maxv
558 1.61 maxv va_start(va, fmt);
559 1.61 maxv size = vsnprintf(str, len + 1, fmt, va);
560 1.61 maxv va_end(va);
561 1.61 maxv
562 1.61 maxv KASSERT(size == len);
563 1.61 maxv
564 1.61 maxv return str;
565 1.7 yamt }
566 1.7 yamt
567 1.64 christos char *
568 1.64 christos kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
569 1.64 christos {
570 1.64 christos size_t len = strlen(str) + 1;
571 1.64 christos char *ptr = kmem_alloc(len, flags);
572 1.64 christos if (ptr == NULL)
573 1.64 christos return NULL;
574 1.64 christos
575 1.64 christos if (lenp)
576 1.64 christos *lenp = len;
577 1.64 christos memcpy(ptr, str, len);
578 1.64 christos return ptr;
579 1.64 christos }
580 1.64 christos
581 1.66 christos char *
582 1.66 christos kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
583 1.66 christos {
584 1.66 christos KASSERT(str != NULL);
585 1.66 christos KASSERT(maxlen != 0);
586 1.66 christos
587 1.66 christos size_t len = strnlen(str, maxlen);
588 1.66 christos char *ptr = kmem_alloc(len + 1, flags);
589 1.66 christos if (ptr == NULL)
590 1.66 christos return NULL;
591 1.66 christos
592 1.66 christos memcpy(ptr, str, len);
593 1.66 christos ptr[len] = '\0';
594 1.66 christos
595 1.66 christos return ptr;
596 1.66 christos }
597 1.66 christos
598 1.64 christos void
599 1.64 christos kmem_strfree(char *str)
600 1.64 christos {
601 1.64 christos if (str == NULL)
602 1.64 christos return;
603 1.64 christos
604 1.64 christos kmem_free(str, strlen(str) + 1);
605 1.64 christos }
606 1.64 christos
607 1.81 thorpej /*
608 1.81 thorpej * Utility routine to maybe-allocate a temporary buffer if the size
609 1.81 thorpej * is larger than we're willing to put on the stack.
610 1.81 thorpej */
611 1.81 thorpej void *
612 1.81 thorpej kmem_tmpbuf_alloc(size_t size, void *stackbuf, size_t stackbufsize,
613 1.81 thorpej km_flag_t flags)
614 1.81 thorpej {
615 1.81 thorpej if (size <= stackbufsize) {
616 1.81 thorpej return stackbuf;
617 1.81 thorpej }
618 1.81 thorpej
619 1.81 thorpej return kmem_alloc(size, flags);
620 1.81 thorpej }
621 1.81 thorpej
622 1.81 thorpej void
623 1.81 thorpej kmem_tmpbuf_free(void *buf, size_t size, void *stackbuf)
624 1.81 thorpej {
625 1.81 thorpej if (buf != stackbuf) {
626 1.81 thorpej kmem_free(buf, size);
627 1.81 thorpej }
628 1.81 thorpej }
629 1.81 thorpej
630 1.76 maxv /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */
631 1.4 yamt
632 1.23 ad #if defined(KMEM_SIZE)
633 1.23 ad static void
634 1.23 ad kmem_size_set(void *p, size_t sz)
635 1.23 ad {
636 1.82 joerg memcpy((char *)p + sz, &sz, sizeof(size_t));
637 1.23 ad }
638 1.23 ad
639 1.23 ad static void
640 1.39 para kmem_size_check(void *p, size_t sz)
641 1.23 ad {
642 1.57 maxv size_t hsz;
643 1.23 ad
644 1.82 joerg memcpy(&hsz, (char *)p + sz, sizeof(size_t));
645 1.57 maxv
646 1.57 maxv if (hsz != sz) {
647 1.79 ad panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?",
648 1.79 ad p, sz, hsz);
649 1.23 ad }
650 1.73 maxv
651 1.82 joerg memset((char *)p + sz, 0xff, sizeof(size_t));
652 1.23 ad }
653 1.54 maxv #endif /* defined(KMEM_SIZE) */
654