subr_kmem.c revision 1.77.2.1 1 /* $NetBSD: subr_kmem.c,v 1.77.2.1 2020/01/25 22:38:51 ad 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 * Prefix each allocations with a fixed-sized, aligned header and record
66 * the exact user-requested allocation size in it. When freeing, compare
67 * it with kmem_free's "size" argument.
68 *
69 * This option is enabled on DIAGNOSTIC.
70 *
71 * |CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|CHUNK|
72 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+---+-+
73 * |/////| | | | | | | | | |U|
74 * |/HSZ/| | | | | | | | | |U|
75 * |/////| | | | | | | | | |U|
76 * +-----+-----+-----+-----+-----+-----+-----+-----+-----+---+-+
77 * |Size | Buffer usable by the caller (requested size) |Unused\
78 */
79
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.77.2.1 2020/01/25 22:38:51 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
97 #include <uvm/uvm_extern.h>
98 #include <uvm/uvm_map.h>
99
100 #include <lib/libkern/libkern.h>
101
102 struct kmem_cache_info {
103 size_t kc_size;
104 const char * kc_name;
105 };
106
107 static const struct kmem_cache_info kmem_cache_sizes[] = {
108 { 8, "kmem-00008" },
109 { 16, "kmem-00016" },
110 { 24, "kmem-00024" },
111 { 32, "kmem-00032" },
112 { 40, "kmem-00040" },
113 { 48, "kmem-00048" },
114 { 56, "kmem-00056" },
115 { 64, "kmem-00064" },
116 { 80, "kmem-00080" },
117 { 96, "kmem-00096" },
118 { 112, "kmem-00112" },
119 { 128, "kmem-00128" },
120 { 160, "kmem-00160" },
121 { 192, "kmem-00192" },
122 { 224, "kmem-00224" },
123 { 256, "kmem-00256" },
124 { 320, "kmem-00320" },
125 { 384, "kmem-00384" },
126 { 448, "kmem-00448" },
127 { 512, "kmem-00512" },
128 { 768, "kmem-00768" },
129 { 1024, "kmem-01024" },
130 { 0, NULL }
131 };
132
133 static const struct kmem_cache_info kmem_cache_big_sizes[] = {
134 { 2048, "kmem-02048" },
135 { 4096, "kmem-04096" },
136 { 8192, "kmem-08192" },
137 { 16384, "kmem-16384" },
138 { 0, NULL }
139 };
140
141 /*
142 * KMEM_ALIGN is the smallest guaranteed alignment and also the
143 * smallest allocateable quantum.
144 * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
145 */
146 #define KMEM_ALIGN 8
147 #define KMEM_SHIFT 3
148 #define KMEM_MAXSIZE 1024
149 #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
150
151 static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
152 static size_t kmem_cache_maxidx __read_mostly;
153
154 #define KMEM_BIG_ALIGN 2048
155 #define KMEM_BIG_SHIFT 11
156 #define KMEM_BIG_MAXSIZE 16384
157 #define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
158
159 static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
160 static size_t kmem_cache_big_maxidx __read_mostly;
161
162 #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
163 #define KMEM_SIZE
164 #endif
165
166 #if defined(DEBUG) && defined(_HARDKERNEL)
167 static void *kmem_freecheck;
168 #endif
169
170 #if defined(KMEM_SIZE)
171 struct kmem_header {
172 size_t size;
173 } __aligned(KMEM_ALIGN);
174 #define SIZE_SIZE sizeof(struct kmem_header)
175 static void kmem_size_set(void *, size_t);
176 static void kmem_size_check(void *, size_t);
177 #else
178 #define SIZE_SIZE 0
179 #define kmem_size_set(p, sz) /* nothing */
180 #define kmem_size_check(p, sz) /* nothing */
181 #endif
182
183 CTASSERT(KM_SLEEP == PR_WAITOK);
184 CTASSERT(KM_NOSLEEP == PR_NOWAIT);
185
186 /*
187 * kmem_intr_alloc: allocate wired memory.
188 */
189 void *
190 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
191 {
192 #ifdef KASAN
193 const size_t origsize = requested_size;
194 #endif
195 size_t allocsz, index;
196 size_t size;
197 pool_cache_t pc;
198 uint8_t *p;
199
200 KASSERT(requested_size > 0);
201
202 KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
203 KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
204
205 kasan_add_redzone(&requested_size);
206 size = kmem_roundup_size(requested_size);
207 allocsz = size + SIZE_SIZE;
208
209 if ((index = ((allocsz -1) >> KMEM_SHIFT))
210 < kmem_cache_maxidx) {
211 pc = kmem_cache[index];
212 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
213 < kmem_cache_big_maxidx) {
214 pc = kmem_cache_big[index];
215 } else {
216 int ret = uvm_km_kmem_alloc(kmem_va_arena,
217 (vsize_t)round_page(size),
218 ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
219 | VM_INSTANTFIT, (vmem_addr_t *)&p);
220 if (ret) {
221 return NULL;
222 }
223 FREECHECK_OUT(&kmem_freecheck, p);
224 return p;
225 }
226
227 p = pool_cache_get(pc, kmflags);
228
229 if (__predict_true(p != NULL)) {
230 FREECHECK_OUT(&kmem_freecheck, p);
231 kmem_size_set(p, requested_size);
232 p += SIZE_SIZE;
233 kasan_mark(p, origsize, size, KASAN_KMEM_REDZONE);
234 return p;
235 }
236 return p;
237 }
238
239 /*
240 * kmem_intr_zalloc: allocate zeroed wired memory.
241 */
242 void *
243 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
244 {
245 void *p;
246
247 p = kmem_intr_alloc(size, kmflags);
248 if (p != NULL) {
249 memset(p, 0, size);
250 }
251 return p;
252 }
253
254 /*
255 * kmem_intr_free: free wired memory allocated by kmem_alloc.
256 */
257 void
258 kmem_intr_free(void *p, size_t requested_size)
259 {
260 size_t allocsz, index;
261 size_t size;
262 pool_cache_t pc;
263
264 KASSERT(p != NULL);
265 KASSERT(requested_size > 0);
266
267 kasan_add_redzone(&requested_size);
268 size = kmem_roundup_size(requested_size);
269 allocsz = size + SIZE_SIZE;
270
271 if ((index = ((allocsz -1) >> KMEM_SHIFT))
272 < kmem_cache_maxidx) {
273 pc = kmem_cache[index];
274 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
275 < kmem_cache_big_maxidx) {
276 pc = kmem_cache_big[index];
277 } else {
278 FREECHECK_IN(&kmem_freecheck, p);
279 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
280 round_page(size));
281 return;
282 }
283
284 kasan_mark(p, size, size, 0);
285
286 p = (uint8_t *)p - SIZE_SIZE;
287 kmem_size_check(p, requested_size);
288 FREECHECK_IN(&kmem_freecheck, p);
289 LOCKDEBUG_MEM_CHECK(p, size);
290
291 pool_cache_put(pc, p);
292 }
293
294 /* -------------------------------- Kmem API -------------------------------- */
295
296 /*
297 * kmem_alloc: allocate wired memory.
298 * => must not be called from interrupt context.
299 */
300 void *
301 kmem_alloc(size_t size, km_flag_t kmflags)
302 {
303 void *v;
304
305 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
306 "kmem(9) should not be used from the interrupt context");
307 v = kmem_intr_alloc(size, kmflags);
308 if (__predict_true(v != NULL)) {
309 kmsan_mark(v, size, KMSAN_STATE_UNINIT);
310 kmsan_orig(v, size, KMSAN_TYPE_KMEM, __RET_ADDR);
311 }
312 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
313 return v;
314 }
315
316 /*
317 * kmem_zalloc: allocate zeroed wired memory.
318 * => must not be called from interrupt context.
319 */
320 void *
321 kmem_zalloc(size_t size, km_flag_t kmflags)
322 {
323 void *v;
324
325 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
326 "kmem(9) should not be used from the interrupt context");
327 v = kmem_intr_zalloc(size, kmflags);
328 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
329 return v;
330 }
331
332 /*
333 * kmem_free: free wired memory allocated by kmem_alloc.
334 * => must not be called from interrupt context.
335 */
336 void
337 kmem_free(void *p, size_t size)
338 {
339 KASSERT(!cpu_intr_p());
340 KASSERT(!cpu_softintr_p());
341 kmem_intr_free(p, size);
342 kmsan_mark(p, size, KMSAN_STATE_INITED);
343 }
344
345 static size_t
346 kmem_create_caches(const struct kmem_cache_info *array,
347 pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
348 {
349 size_t maxidx = 0;
350 size_t table_unit = (1 << shift);
351 size_t size = table_unit;
352 int i;
353
354 for (i = 0; array[i].kc_size != 0 ; i++) {
355 const char *name = array[i].kc_name;
356 size_t cache_size = array[i].kc_size;
357 struct pool_allocator *pa;
358 int flags = 0;
359 pool_cache_t pc;
360 size_t align;
361
362 /* check if we reached the requested size */
363 if (cache_size > maxsize || cache_size > PAGE_SIZE) {
364 break;
365 }
366
367 /*
368 * Exclude caches with size not a factor or multiple of the
369 * coherency unit.
370 */
371 if (cache_size < COHERENCY_UNIT) {
372 if (COHERENCY_UNIT % cache_size > 0) {
373 continue;
374 }
375 flags |= PR_NOTOUCH;
376 align = KMEM_ALIGN;
377 } else if ((cache_size & (PAGE_SIZE - 1)) == 0) {
378 align = PAGE_SIZE;
379 } else {
380 if ((cache_size % COHERENCY_UNIT) > 0) {
381 continue;
382 }
383 align = COHERENCY_UNIT;
384 }
385
386 if ((cache_size >> shift) > maxidx) {
387 maxidx = cache_size >> shift;
388 }
389
390 pa = &pool_allocator_kmem;
391 pc = pool_cache_init(cache_size, align, 0, flags,
392 name, pa, ipl, NULL, NULL, NULL);
393
394 while (size <= cache_size) {
395 alloc_table[(size - 1) >> shift] = pc;
396 size += table_unit;
397 }
398 }
399 return maxidx;
400 }
401
402 void
403 kmem_init(void)
404 {
405 kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
406 kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
407 kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
408 kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
409 }
410
411 size_t
412 kmem_roundup_size(size_t size)
413 {
414 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
415 }
416
417 /*
418 * Used to dynamically allocate string with kmem accordingly to format.
419 */
420 char *
421 kmem_asprintf(const char *fmt, ...)
422 {
423 int size __diagused, len;
424 va_list va;
425 char *str;
426
427 va_start(va, fmt);
428 len = vsnprintf(NULL, 0, fmt, va);
429 va_end(va);
430
431 str = kmem_alloc(len + 1, KM_SLEEP);
432
433 va_start(va, fmt);
434 size = vsnprintf(str, len + 1, fmt, va);
435 va_end(va);
436
437 KASSERT(size == len);
438
439 return str;
440 }
441
442 char *
443 kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
444 {
445 size_t len = strlen(str) + 1;
446 char *ptr = kmem_alloc(len, flags);
447 if (ptr == NULL)
448 return NULL;
449
450 if (lenp)
451 *lenp = len;
452 memcpy(ptr, str, len);
453 return ptr;
454 }
455
456 char *
457 kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
458 {
459 KASSERT(str != NULL);
460 KASSERT(maxlen != 0);
461
462 size_t len = strnlen(str, maxlen);
463 char *ptr = kmem_alloc(len + 1, flags);
464 if (ptr == NULL)
465 return NULL;
466
467 memcpy(ptr, str, len);
468 ptr[len] = '\0';
469
470 return ptr;
471 }
472
473 void
474 kmem_strfree(char *str)
475 {
476 if (str == NULL)
477 return;
478
479 kmem_free(str, strlen(str) + 1);
480 }
481
482 /* --------------------------- DEBUG / DIAGNOSTIC --------------------------- */
483
484 #if defined(KMEM_SIZE)
485 static void
486 kmem_size_set(void *p, size_t sz)
487 {
488 struct kmem_header *hd;
489 hd = (struct kmem_header *)p;
490 hd->size = sz;
491 }
492
493 static void
494 kmem_size_check(void *p, size_t sz)
495 {
496 struct kmem_header *hd;
497 size_t hsz;
498
499 hd = (struct kmem_header *)p;
500 hsz = hd->size;
501
502 if (hsz != sz) {
503 panic("kmem_free(%p, %zu) != allocated size %zu",
504 (const uint8_t *)p + SIZE_SIZE, sz, hsz);
505 }
506
507 hd->size = -1;
508 }
509 #endif /* defined(KMEM_SIZE) */
510