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