1 /* $NetBSD: subr_kmem.c,v 1.88 2023/04/09 08:50:20 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 2009-2020 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.88 2023/04/09 08:50:20 riastradh 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 return p;
329 }
330
331 if (__predict_true(p != NULL)) {
332 FREECHECK_OUT(&kmem_freecheck, p);
333 kmem_size_set(p, requested_size);
334 kasan_mark(p, origsize, size, KASAN_KMEM_REDZONE);
335 return p;
336 }
337 return p;
338 }
339
340 /*
341 * kmem_intr_zalloc: allocate zeroed wired memory.
342 */
343 void *
kmem_intr_zalloc(size_t size,km_flag_t kmflags)344 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
345 {
346 void *p;
347
348 p = kmem_intr_alloc(size, kmflags);
349 if (__predict_true(p != NULL)) {
350 memset(p, 0, size);
351 }
352 return p;
353 }
354
355 /*
356 * kmem_intr_free: free wired memory allocated by kmem_alloc.
357 */
358 void
kmem_intr_free(void * p,size_t requested_size)359 kmem_intr_free(void *p, size_t requested_size)
360 {
361 size_t allocsz, index;
362 size_t size;
363 pool_cache_t pc;
364
365 KASSERT(p != NULL);
366 KASSERTMSG(requested_size > 0, "kmem_intr_free(%p, 0)", p);
367
368 kasan_add_redzone(&requested_size);
369 size = kmem_roundup_size(requested_size);
370 allocsz = size + SIZE_SIZE;
371
372 if ((index = ((allocsz - 1) >> KMEM_SHIFT))
373 < kmem_cache_maxidx) {
374 KMEM_CACHE_FREE_PROBE(index, p, requested_size, allocsz);
375 pc = kmem_cache[index];
376 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
377 < kmem_cache_big_maxidx) {
378 KMEM_CACHE_BIG_FREE_PROBE(index, p, requested_size, allocsz);
379 pc = kmem_cache_big[index];
380 } else {
381 FREECHECK_IN(&kmem_freecheck, p);
382 SDT_PROBE3(sdt, kmem, free, large,
383 p, requested_size, round_page(size));
384 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
385 round_page(size));
386 return;
387 }
388
389 kasan_mark(p, size, size, 0);
390
391 kmem_size_check(p, requested_size);
392 FREECHECK_IN(&kmem_freecheck, p);
393 LOCKDEBUG_MEM_CHECK(p, size);
394
395 pool_cache_put(pc, p);
396 }
397
398 /* -------------------------------- Kmem API -------------------------------- */
399
400 /*
401 * kmem_alloc: allocate wired memory.
402 * => must not be called from interrupt context.
403 */
404 void *
kmem_alloc(size_t size,km_flag_t kmflags)405 kmem_alloc(size_t size, km_flag_t kmflags)
406 {
407 void *v;
408
409 KASSERT(!cpu_intr_p());
410 KASSERT(!cpu_softintr_p());
411
412 v = kmem_intr_alloc(size, kmflags);
413 if (__predict_true(v != NULL)) {
414 kmsan_mark(v, size, KMSAN_STATE_UNINIT);
415 kmsan_orig(v, size, KMSAN_TYPE_KMEM, __RET_ADDR);
416 }
417 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
418 return v;
419 }
420
421 /*
422 * kmem_zalloc: allocate zeroed wired memory.
423 * => must not be called from interrupt context.
424 */
425 void *
kmem_zalloc(size_t size,km_flag_t kmflags)426 kmem_zalloc(size_t size, km_flag_t kmflags)
427 {
428 void *v;
429
430 KASSERT(!cpu_intr_p());
431 KASSERT(!cpu_softintr_p());
432
433 v = kmem_intr_zalloc(size, kmflags);
434 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
435 return v;
436 }
437
438 /*
439 * kmem_free: free wired memory allocated by kmem_alloc.
440 * => must not be called from interrupt context.
441 */
442 void
kmem_free(void * p,size_t size)443 kmem_free(void *p, size_t size)
444 {
445
446 KASSERT(!cpu_intr_p());
447 KASSERT(!cpu_softintr_p());
448
449 kmem_intr_free(p, size);
450 kmsan_mark(p, size, KMSAN_STATE_INITED);
451 }
452
453 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)454 kmem_create_caches(const struct kmem_cache_info *array,
455 const id_t *alloc_probe_table[], const id_t *free_probe_table[],
456 pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
457 {
458 size_t maxidx = 0;
459 size_t table_unit = (1 << shift);
460 size_t size = table_unit;
461 int i;
462
463 for (i = 0; array[i].kc_size != 0 ; i++) {
464 const char *name = array[i].kc_name;
465 size_t cache_size = array[i].kc_size;
466 struct pool_allocator *pa;
467 int flags = 0;
468 pool_cache_t pc;
469 size_t align;
470
471 /* check if we reached the requested size */
472 if (cache_size > maxsize || cache_size > PAGE_SIZE) {
473 break;
474 }
475
476 /*
477 * Exclude caches with size not a factor or multiple of the
478 * coherency unit.
479 */
480 if (cache_size < COHERENCY_UNIT) {
481 if (COHERENCY_UNIT % cache_size > 0) {
482 continue;
483 }
484 flags |= PR_NOTOUCH;
485 align = KMEM_ALIGN;
486 } else if ((cache_size & (PAGE_SIZE - 1)) == 0) {
487 align = PAGE_SIZE;
488 } else {
489 if ((cache_size % COHERENCY_UNIT) > 0) {
490 continue;
491 }
492 align = COHERENCY_UNIT;
493 }
494
495 if ((cache_size >> shift) > maxidx) {
496 maxidx = cache_size >> shift;
497 }
498
499 pa = &pool_allocator_kmem;
500 pc = pool_cache_init(cache_size, align, 0, flags,
501 name, pa, ipl, NULL, NULL, NULL);
502
503 while (size <= cache_size) {
504 alloc_table[(size - 1) >> shift] = pc;
505 #ifdef KDTRACE_HOOKS
506 if (alloc_probe_table) {
507 alloc_probe_table[(size - 1) >> shift] =
508 array[i].kc_alloc_probe_id;
509 }
510 if (free_probe_table) {
511 free_probe_table[(size - 1) >> shift] =
512 array[i].kc_free_probe_id;
513 }
514 #endif
515 size += table_unit;
516 }
517 }
518 return maxidx;
519 }
520
521 void
kmem_init(void)522 kmem_init(void)
523 {
524 kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
525 kmem_cache_alloc_probe_id, kmem_cache_free_probe_id,
526 kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
527 kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
528 kmem_cache_big_alloc_probe_id, kmem_cache_big_free_probe_id,
529 kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
530 }
531
532 size_t
kmem_roundup_size(size_t size)533 kmem_roundup_size(size_t size)
534 {
535 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
536 }
537
538 /*
539 * Used to dynamically allocate string with kmem accordingly to format.
540 */
541 char *
kmem_asprintf(const char * fmt,...)542 kmem_asprintf(const char *fmt, ...)
543 {
544 int size __diagused, len;
545 va_list va;
546 char *str;
547
548 va_start(va, fmt);
549 len = vsnprintf(NULL, 0, fmt, va);
550 va_end(va);
551
552 str = kmem_alloc(len + 1, KM_SLEEP);
553
554 va_start(va, fmt);
555 size = vsnprintf(str, len + 1, fmt, va);
556 va_end(va);
557
558 KASSERT(size == len);
559
560 return str;
561 }
562
563 char *
kmem_strdupsize(const char * str,size_t * lenp,km_flag_t flags)564 kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
565 {
566 size_t len = strlen(str) + 1;
567 char *ptr = kmem_alloc(len, flags);
568 if (ptr == NULL)
569 return NULL;
570
571 if (lenp)
572 *lenp = len;
573 memcpy(ptr, str, len);
574 return ptr;
575 }
576
577 char *
kmem_strndup(const char * str,size_t maxlen,km_flag_t flags)578 kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
579 {
580 KASSERT(str != NULL);
581 KASSERT(maxlen != 0);
582
583 size_t len = strnlen(str, maxlen);
584 char *ptr = kmem_alloc(len + 1, flags);
585 if (ptr == NULL)
586 return NULL;
587
588 memcpy(ptr, str, len);
589 ptr[len] = '\0';
590
591 return ptr;
592 }
593
594 void
kmem_strfree(char * str)595 kmem_strfree(char *str)
596 {
597 if (str == NULL)
598 return;
599
600 kmem_free(str, strlen(str) + 1);
601 }
602
603 /*
604 * Utility routine to maybe-allocate a temporary buffer if the size
605 * is larger than we're willing to put on the stack.
606 */
607 void *
kmem_tmpbuf_alloc(size_t size,void * stackbuf,size_t stackbufsize,km_flag_t flags)608 kmem_tmpbuf_alloc(size_t size, void *stackbuf, size_t stackbufsize,
609 km_flag_t flags)
610 {
611 if (size <= stackbufsize) {
612 return stackbuf;
613 }
614
615 return kmem_alloc(size, flags);
616 }
617
618 void
kmem_tmpbuf_free(void * buf,size_t size,void * stackbuf)619 kmem_tmpbuf_free(void *buf, size_t size, void *stackbuf)
620 {
621 if (buf != stackbuf) {
622 kmem_free(buf, size);
623 }
624 }
625
626 /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */
627
628 #if defined(KMEM_SIZE)
629 static void
kmem_size_set(void * p,size_t sz)630 kmem_size_set(void *p, size_t sz)
631 {
632 memcpy((char *)p + sz, &sz, sizeof(size_t));
633 }
634
635 static void
kmem_size_check(void * p,size_t sz)636 kmem_size_check(void *p, size_t sz)
637 {
638 size_t hsz;
639
640 memcpy(&hsz, (char *)p + sz, sizeof(size_t));
641
642 if (hsz != sz) {
643 panic("kmem_free(%p, %zu) != allocated size %zu; overwrote?",
644 p, sz, hsz);
645 }
646
647 memset((char *)p + sz, 0xff, sizeof(size_t));
648 }
649 #endif /* defined(KMEM_SIZE) */
650