subr_kmem.c revision 1.45 1 /* $NetBSD: subr_kmem.c,v 1.45 2012/04/15 19:07:40 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2009 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.
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.
60 *
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.45 2012/04/15 19:07:40 martin Exp $");
65
66 #include <sys/param.h>
67 #include <sys/callback.h>
68 #include <sys/kmem.h>
69 #include <sys/pool.h>
70 #include <sys/debug.h>
71 #include <sys/lockdebug.h>
72 #include <sys/cpu.h>
73
74 #include <uvm/uvm_extern.h>
75 #include <uvm/uvm_map.h>
76 #include <uvm/uvm_kmguard.h>
77
78 #include <lib/libkern/libkern.h>
79
80 static const struct kmem_cache_info {
81 size_t kc_size;
82 const char * kc_name;
83 } kmem_cache_sizes[] = {
84 { 8, "kmem-8" },
85 { 16, "kmem-16" },
86 { 24, "kmem-24" },
87 { 32, "kmem-32" },
88 { 40, "kmem-40" },
89 { 48, "kmem-48" },
90 { 56, "kmem-56" },
91 { 64, "kmem-64" },
92 { 80, "kmem-80" },
93 { 96, "kmem-96" },
94 { 112, "kmem-112" },
95 { 128, "kmem-128" },
96 { 160, "kmem-160" },
97 { 192, "kmem-192" },
98 { 224, "kmem-224" },
99 { 256, "kmem-256" },
100 { 320, "kmem-320" },
101 { 384, "kmem-384" },
102 { 448, "kmem-448" },
103 { 512, "kmem-512" },
104 { 768, "kmem-768" },
105 { 1024, "kmem-1024" },
106 { 2048, "kmem-2048" },
107 { 4096, "kmem-4096" },
108 { 0, NULL }
109 };
110
111 /*
112 * KMEM_ALIGN is the smallest guaranteed alignment and also the
113 * smallest allocateable quantum. Every cache size is a multiply
114 * of CACHE_LINE_SIZE and gets CACHE_LINE_SIZE alignment.
115 */
116 #define KMEM_ALIGN 8
117 #define KMEM_SHIFT 3
118 #define KMEM_MAXSIZE 4096
119 #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
120
121 static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
122 static size_t kmem_cache_maxidx __read_mostly;
123
124 #if defined(DEBUG) && defined(_HARDKERNEL)
125 #ifndef KMEM_GUARD_DEPTH
126 #define KMEM_GUARD_DEPTH 0
127 #endif
128 int kmem_guard_depth = KMEM_GUARD_DEPTH;
129 size_t kmem_guard_size;
130 static struct uvm_kmguard kmem_guard;
131 static void *kmem_freecheck;
132 #define KMEM_POISON
133 #define KMEM_REDZONE
134 #define KMEM_SIZE
135 #define KMEM_GUARD
136 #endif /* defined(DEBUG) */
137
138 #if defined(KMEM_POISON)
139 static int kmem_poison_ctor(void *, void *, int);
140 static void kmem_poison_fill(void *, size_t);
141 static void kmem_poison_check(void *, size_t);
142 #else /* defined(KMEM_POISON) */
143 #define kmem_poison_fill(p, sz) /* nothing */
144 #define kmem_poison_check(p, sz) /* nothing */
145 #endif /* defined(KMEM_POISON) */
146
147 #if defined(KMEM_REDZONE)
148 #define REDZONE_SIZE 1
149 #else /* defined(KMEM_REDZONE) */
150 #define REDZONE_SIZE 0
151 #endif /* defined(KMEM_REDZONE) */
152
153 #if defined(KMEM_SIZE)
154 #define SIZE_SIZE (MAX(KMEM_ALIGN, sizeof(size_t)))
155 static void kmem_size_set(void *, size_t);
156 static void kmem_size_check(void *, size_t);
157 #else
158 #define SIZE_SIZE 0
159 #define kmem_size_set(p, sz) /* nothing */
160 #define kmem_size_check(p, sz) /* nothing */
161 #endif
162
163 CTASSERT(KM_SLEEP == PR_WAITOK);
164 CTASSERT(KM_NOSLEEP == PR_NOWAIT);
165
166 void *
167 kmem_intr_alloc(size_t size, km_flag_t kmflags)
168 {
169 size_t allocsz, index;
170 pool_cache_t pc;
171 uint8_t *p;
172
173 KASSERT(size > 0);
174
175 #ifdef KMEM_GUARD
176 if (size <= kmem_guard_size) {
177 return uvm_kmguard_alloc(&kmem_guard, size,
178 (kmflags & KM_SLEEP) != 0);
179 }
180 #endif
181 allocsz = kmem_roundup_size(size) + REDZONE_SIZE + SIZE_SIZE;
182 index = (allocsz - 1) >> KMEM_SHIFT;
183
184 if (index >= kmem_cache_maxidx) {
185 int ret = uvm_km_kmem_alloc(kmem_va_arena,
186 (vsize_t)round_page(size),
187 ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
188 | VM_INSTANTFIT, (vmem_addr_t *)&p);
189 return ret ? NULL : p;
190 }
191
192 pc = kmem_cache[index];
193 p = pool_cache_get(pc, kmflags);
194
195 if (__predict_true(p != NULL)) {
196 kmem_poison_check(p, kmem_roundup_size(size));
197 FREECHECK_OUT(&kmem_freecheck, p);
198 kmem_size_set(p, allocsz);
199 }
200 return p;
201 }
202
203 void *
204 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
205 {
206 void *p;
207
208 p = kmem_intr_alloc(size, kmflags);
209 if (p != NULL) {
210 memset(p, 0, size);
211 }
212 return p;
213 }
214
215 void
216 kmem_intr_free(void *p, size_t size)
217 {
218 size_t allocsz, index;
219 pool_cache_t pc;
220
221 KASSERT(p != NULL);
222 KASSERT(size > 0);
223
224 #ifdef KMEM_GUARD
225 if (size <= kmem_guard_size) {
226 uvm_kmguard_free(&kmem_guard, size, p);
227 return;
228 }
229 #endif
230 allocsz = kmem_roundup_size(size) + REDZONE_SIZE + SIZE_SIZE;
231 index = (allocsz - 1) >> KMEM_SHIFT;
232
233 if (index >= kmem_cache_maxidx) {
234 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
235 round_page(size));
236 return;
237 }
238
239 kmem_size_check(p, allocsz);
240 FREECHECK_IN(&kmem_freecheck, p);
241 LOCKDEBUG_MEM_CHECK(p, allocsz - (REDZONE_SIZE + SIZE_SIZE));
242 kmem_poison_check((uint8_t *)p + size, allocsz - size - SIZE_SIZE);
243 kmem_poison_fill(p, allocsz);
244
245 pc = kmem_cache[index];
246 pool_cache_put(pc, p);
247 }
248
249 /* ---- kmem API */
250
251 /*
252 * kmem_alloc: allocate wired memory.
253 * => must not be called from interrupt context.
254 */
255
256 void *
257 kmem_alloc(size_t size, km_flag_t kmflags)
258 {
259
260 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
261 "kmem(9) should not be used from the interrupt context");
262 return kmem_intr_alloc(size, kmflags);
263 }
264
265 /*
266 * kmem_zalloc: allocate zeroed wired memory.
267 * => must not be called from interrupt context.
268 */
269
270 void *
271 kmem_zalloc(size_t size, km_flag_t kmflags)
272 {
273
274 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
275 "kmem(9) should not be used from the interrupt context");
276 return kmem_intr_zalloc(size, kmflags);
277 }
278
279 /*
280 * kmem_free: free wired memory allocated by kmem_alloc.
281 * => must not be called from interrupt context.
282 */
283
284 void
285 kmem_free(void *p, size_t size)
286 {
287
288 KASSERT(!cpu_intr_p());
289 KASSERT(!cpu_softintr_p());
290 kmem_intr_free(p, size);
291 }
292
293 static void
294 kmem_create_caches(const struct kmem_cache_info *array,
295 pool_cache_t alloc_table[], size_t maxsize)
296 {
297 size_t table_unit = (1 << KMEM_SHIFT);
298 size_t size = table_unit;
299 int i;
300
301 for (i = 0; array[i].kc_size != 0 ; i++) {
302 const char *name = array[i].kc_name;
303 size_t cache_size = array[i].kc_size;
304 int flags = PR_NOALIGN;
305 pool_cache_t pc;
306 size_t align;
307
308 if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
309 align = CACHE_LINE_SIZE;
310 else if ((cache_size & (PAGE_SIZE - 1)) == 0)
311 align = PAGE_SIZE;
312 else
313 align = KMEM_ALIGN;
314
315 if (cache_size < CACHE_LINE_SIZE)
316 flags |= PR_NOTOUCH;
317
318 /* check if we reached the requested size */
319 if (cache_size > maxsize) {
320 break;
321 }
322 if ((cache_size >> KMEM_SHIFT) > kmem_cache_maxidx) {
323 kmem_cache_maxidx = cache_size >> KMEM_SHIFT;
324 }
325
326 #if defined(KMEM_POISON)
327 pc = pool_cache_init(cache_size, align, 0, flags,
328 name, &pool_allocator_kmem, IPL_VM, kmem_poison_ctor,
329 NULL, (void *)cache_size);
330 #else /* defined(KMEM_POISON) */
331 pc = pool_cache_init(cache_size, align, 0, flags,
332 name, &pool_allocator_kmem, IPL_VM, NULL, NULL, NULL);
333 #endif /* defined(KMEM_POISON) */
334
335 while (size <= cache_size) {
336 alloc_table[(size - 1) >> KMEM_SHIFT] = pc;
337 size += table_unit;
338 }
339 }
340 }
341
342 void
343 kmem_init(void)
344 {
345
346 #ifdef KMEM_GUARD
347 uvm_kmguard_init(&kmem_guard, &kmem_guard_depth, &kmem_guard_size,
348 kmem_va_arena);
349 #endif
350 kmem_create_caches(kmem_cache_sizes, kmem_cache, KMEM_MAXSIZE);
351 }
352
353 size_t
354 kmem_roundup_size(size_t size)
355 {
356
357 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
358 }
359
360 /* ---- debug */
361
362 #if defined(KMEM_POISON)
363
364 #if defined(_LP64)
365 #define PRIME 0x9e37fffffffc0000UL
366 #else /* defined(_LP64) */
367 #define PRIME 0x9e3779b1
368 #endif /* defined(_LP64) */
369
370 static inline uint8_t
371 kmem_poison_pattern(const void *p)
372 {
373
374 return (uint8_t)(((uintptr_t)p) * PRIME
375 >> ((sizeof(uintptr_t) - sizeof(uint8_t))) * CHAR_BIT);
376 }
377
378 static int
379 kmem_poison_ctor(void *arg, void *obj, int flag)
380 {
381 size_t sz = (size_t)arg;
382
383 kmem_poison_fill(obj, sz);
384
385 return 0;
386 }
387
388 static void
389 kmem_poison_fill(void *p, size_t sz)
390 {
391 uint8_t *cp;
392 const uint8_t *ep;
393
394 cp = p;
395 ep = cp + sz;
396 while (cp < ep) {
397 *cp = kmem_poison_pattern(cp);
398 cp++;
399 }
400 }
401
402 static void
403 kmem_poison_check(void *p, size_t sz)
404 {
405 uint8_t *cp;
406 const uint8_t *ep;
407
408 cp = p;
409 ep = cp + sz;
410 while (cp < ep) {
411 const uint8_t expected = kmem_poison_pattern(cp);
412
413 if (*cp != expected) {
414 panic("%s: %p: 0x%02x != 0x%02x\n",
415 __func__, cp, *cp, expected);
416 }
417 cp++;
418 }
419 }
420
421 #endif /* defined(KMEM_POISON) */
422
423 #if defined(KMEM_SIZE)
424 static void
425 kmem_size_set(void *p, size_t sz)
426 {
427 void *szp;
428
429 szp = (uint8_t *)p + sz - SIZE_SIZE;
430 memcpy(szp, &sz, sizeof(sz));
431 }
432
433 static void
434 kmem_size_check(void *p, size_t sz)
435 {
436 uint8_t *szp;
437 size_t psz;
438
439 szp = (uint8_t *)p + sz - SIZE_SIZE;
440 memcpy(&psz, szp, sizeof(psz));
441 if (psz != sz) {
442 panic("kmem_free(%p, %zu) != allocated size %zu",
443 (const uint8_t *)p + SIZE_SIZE, sz - SIZE_SIZE, psz);
444 }
445 }
446 #endif /* defined(KMEM_SIZE) */
447
448 /*
449 * Used to dynamically allocate string with kmem accordingly to format.
450 */
451 char *
452 kmem_asprintf(const char *fmt, ...)
453 {
454 int size, len;
455 va_list va;
456 char *str;
457
458 va_start(va, fmt);
459 len = vsnprintf(NULL, 0, fmt, va);
460 va_end(va);
461
462 str = kmem_alloc(len + 1, KM_SLEEP);
463
464 va_start(va, fmt);
465 size = vsnprintf(str, len + 1, fmt, va);
466 va_end(va);
467
468 KASSERT(size == len);
469
470 return str;
471 }
472