subr_kmem.c revision 1.70 1 /* $NetBSD: subr_kmem.c,v 1.70 2018/08/22 09:38:21 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 2009-2015 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 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 /*
81 * KMEM_GUARD
82 * A kernel with "option DEBUG" has "kmem_guard" debugging feature compiled
83 * in. See the comment below for what kind of bugs it tries to detect. Even
84 * if compiled in, it's disabled by default because it's very expensive.
85 * You can enable it on boot by:
86 * boot -d
87 * db> w kmem_guard_depth 0t30000
88 * db> c
89 *
90 * The default value of kmem_guard_depth is 0, which means disabled.
91 * It can be changed by KMEM_GUARD_DEPTH kernel config option.
92 */
93
94 #include <sys/cdefs.h>
95 __KERNEL_RCSID(0, "$NetBSD: subr_kmem.c,v 1.70 2018/08/22 09:38:21 maxv Exp $");
96
97 #ifdef _KERNEL_OPT
98 #include "opt_kmem.h"
99 #include "opt_kasan.h"
100 #endif
101
102 #include <sys/param.h>
103 #include <sys/callback.h>
104 #include <sys/kmem.h>
105 #include <sys/pool.h>
106 #include <sys/debug.h>
107 #include <sys/lockdebug.h>
108 #include <sys/cpu.h>
109 #include <sys/asan.h>
110
111 #include <uvm/uvm_extern.h>
112 #include <uvm/uvm_map.h>
113
114 #include <lib/libkern/libkern.h>
115
116 struct kmem_cache_info {
117 size_t kc_size;
118 const char * kc_name;
119 };
120
121 static const struct kmem_cache_info kmem_cache_sizes[] = {
122 { 8, "kmem-8" },
123 { 16, "kmem-16" },
124 { 24, "kmem-24" },
125 { 32, "kmem-32" },
126 { 40, "kmem-40" },
127 { 48, "kmem-48" },
128 { 56, "kmem-56" },
129 { 64, "kmem-64" },
130 { 80, "kmem-80" },
131 { 96, "kmem-96" },
132 { 112, "kmem-112" },
133 { 128, "kmem-128" },
134 { 160, "kmem-160" },
135 { 192, "kmem-192" },
136 { 224, "kmem-224" },
137 { 256, "kmem-256" },
138 { 320, "kmem-320" },
139 { 384, "kmem-384" },
140 { 448, "kmem-448" },
141 { 512, "kmem-512" },
142 { 768, "kmem-768" },
143 { 1024, "kmem-1024" },
144 { 0, NULL }
145 };
146
147 static const struct kmem_cache_info kmem_cache_big_sizes[] = {
148 { 2048, "kmem-2048" },
149 { 4096, "kmem-4096" },
150 { 8192, "kmem-8192" },
151 { 16384, "kmem-16384" },
152 { 0, NULL }
153 };
154
155 /*
156 * KMEM_ALIGN is the smallest guaranteed alignment and also the
157 * smallest allocateable quantum.
158 * Every cache size >= CACHE_LINE_SIZE gets CACHE_LINE_SIZE alignment.
159 */
160 #define KMEM_ALIGN 8
161 #define KMEM_SHIFT 3
162 #define KMEM_MAXSIZE 1024
163 #define KMEM_CACHE_COUNT (KMEM_MAXSIZE >> KMEM_SHIFT)
164
165 static pool_cache_t kmem_cache[KMEM_CACHE_COUNT] __cacheline_aligned;
166 static size_t kmem_cache_maxidx __read_mostly;
167
168 #define KMEM_BIG_ALIGN 2048
169 #define KMEM_BIG_SHIFT 11
170 #define KMEM_BIG_MAXSIZE 16384
171 #define KMEM_CACHE_BIG_COUNT (KMEM_BIG_MAXSIZE >> KMEM_BIG_SHIFT)
172
173 static pool_cache_t kmem_cache_big[KMEM_CACHE_BIG_COUNT] __cacheline_aligned;
174 static size_t kmem_cache_big_maxidx __read_mostly;
175
176 #if defined(DIAGNOSTIC) && defined(_HARDKERNEL)
177 #define KMEM_SIZE
178 #endif
179
180 #if defined(DEBUG) && defined(_HARDKERNEL)
181 #define KMEM_SIZE
182 #define KMEM_GUARD
183 static void *kmem_freecheck;
184 #endif
185
186 #if defined(KMEM_SIZE)
187 struct kmem_header {
188 size_t size;
189 } __aligned(KMEM_ALIGN);
190 #define SIZE_SIZE sizeof(struct kmem_header)
191 static void kmem_size_set(void *, size_t);
192 static void kmem_size_check(void *, size_t);
193 #else
194 #define SIZE_SIZE 0
195 #define kmem_size_set(p, sz) /* nothing */
196 #define kmem_size_check(p, sz) /* nothing */
197 #endif
198
199 #if defined(KMEM_GUARD)
200 #ifndef KMEM_GUARD_DEPTH
201 #define KMEM_GUARD_DEPTH 0
202 #endif
203 struct kmem_guard {
204 u_int kg_depth;
205 intptr_t * kg_fifo;
206 u_int kg_rotor;
207 vmem_t * kg_vmem;
208 };
209 static bool kmem_guard_init(struct kmem_guard *, u_int, vmem_t *);
210 static void *kmem_guard_alloc(struct kmem_guard *, size_t, bool);
211 static void kmem_guard_free(struct kmem_guard *, size_t, void *);
212 int kmem_guard_depth = KMEM_GUARD_DEPTH;
213 static bool kmem_guard_enabled;
214 static struct kmem_guard kmem_guard;
215 #endif /* defined(KMEM_GUARD) */
216
217 CTASSERT(KM_SLEEP == PR_WAITOK);
218 CTASSERT(KM_NOSLEEP == PR_NOWAIT);
219
220 /*
221 * kmem_intr_alloc: allocate wired memory.
222 */
223
224 void *
225 kmem_intr_alloc(size_t requested_size, km_flag_t kmflags)
226 {
227 const size_t origsize = requested_size;
228 size_t allocsz, index;
229 size_t size;
230 pool_cache_t pc;
231 uint8_t *p;
232
233 KASSERT(requested_size > 0);
234
235 KASSERT((kmflags & KM_SLEEP) || (kmflags & KM_NOSLEEP));
236 KASSERT(!(kmflags & KM_SLEEP) || !(kmflags & KM_NOSLEEP));
237
238 #ifdef KMEM_GUARD
239 if (kmem_guard_enabled) {
240 return kmem_guard_alloc(&kmem_guard, requested_size,
241 (kmflags & KM_SLEEP) != 0);
242 }
243 #endif
244
245 kasan_add_redzone(&requested_size);
246 size = kmem_roundup_size(requested_size);
247 allocsz = size + SIZE_SIZE;
248
249 if ((index = ((allocsz -1) >> KMEM_SHIFT))
250 < kmem_cache_maxidx) {
251 pc = kmem_cache[index];
252 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
253 < kmem_cache_big_maxidx) {
254 pc = kmem_cache_big[index];
255 } else {
256 int ret = uvm_km_kmem_alloc(kmem_va_arena,
257 (vsize_t)round_page(size),
258 ((kmflags & KM_SLEEP) ? VM_SLEEP : VM_NOSLEEP)
259 | VM_INSTANTFIT, (vmem_addr_t *)&p);
260 if (ret) {
261 return NULL;
262 }
263 FREECHECK_OUT(&kmem_freecheck, p);
264 return p;
265 }
266
267 p = pool_cache_get(pc, kmflags);
268
269 if (__predict_true(p != NULL)) {
270 FREECHECK_OUT(&kmem_freecheck, p);
271 kmem_size_set(p, requested_size);
272 p += SIZE_SIZE;
273 kasan_alloc(p, origsize, size);
274 return p;
275 }
276 return p;
277 }
278
279 /*
280 * kmem_intr_zalloc: allocate zeroed wired memory.
281 */
282
283 void *
284 kmem_intr_zalloc(size_t size, km_flag_t kmflags)
285 {
286 void *p;
287
288 p = kmem_intr_alloc(size, kmflags);
289 if (p != NULL) {
290 memset(p, 0, size);
291 }
292 return p;
293 }
294
295 /*
296 * kmem_intr_free: free wired memory allocated by kmem_alloc.
297 */
298
299 void
300 kmem_intr_free(void *p, size_t requested_size)
301 {
302 size_t allocsz, index;
303 size_t size;
304 pool_cache_t pc;
305
306 KASSERT(p != NULL);
307 KASSERT(requested_size > 0);
308
309 #ifdef KMEM_GUARD
310 if (kmem_guard_enabled) {
311 kmem_guard_free(&kmem_guard, requested_size, p);
312 return;
313 }
314 #endif
315
316 kasan_add_redzone(&requested_size);
317 size = kmem_roundup_size(requested_size);
318 allocsz = size + SIZE_SIZE;
319
320 if ((index = ((allocsz -1) >> KMEM_SHIFT))
321 < kmem_cache_maxidx) {
322 pc = kmem_cache[index];
323 } else if ((index = ((allocsz - 1) >> KMEM_BIG_SHIFT))
324 < kmem_cache_big_maxidx) {
325 pc = kmem_cache_big[index];
326 } else {
327 FREECHECK_IN(&kmem_freecheck, p);
328 uvm_km_kmem_free(kmem_va_arena, (vaddr_t)p,
329 round_page(size));
330 return;
331 }
332
333 kasan_free(p, size);
334
335 p = (uint8_t *)p - SIZE_SIZE;
336 kmem_size_check(p, requested_size);
337 FREECHECK_IN(&kmem_freecheck, p);
338 LOCKDEBUG_MEM_CHECK(p, size);
339
340 pool_cache_put(pc, p);
341 }
342
343 /* ---- kmem API */
344
345 /*
346 * kmem_alloc: allocate wired memory.
347 * => must not be called from interrupt context.
348 */
349
350 void *
351 kmem_alloc(size_t size, km_flag_t kmflags)
352 {
353 void *v;
354
355 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
356 "kmem(9) should not be used from the interrupt context");
357 v = kmem_intr_alloc(size, kmflags);
358 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
359 return v;
360 }
361
362 /*
363 * kmem_zalloc: allocate zeroed wired memory.
364 * => must not be called from interrupt context.
365 */
366
367 void *
368 kmem_zalloc(size_t size, km_flag_t kmflags)
369 {
370 void *v;
371
372 KASSERTMSG((!cpu_intr_p() && !cpu_softintr_p()),
373 "kmem(9) should not be used from the interrupt context");
374 v = kmem_intr_zalloc(size, kmflags);
375 KASSERT(v || (kmflags & KM_NOSLEEP) != 0);
376 return v;
377 }
378
379 /*
380 * kmem_free: free wired memory allocated by kmem_alloc.
381 * => must not be called from interrupt context.
382 */
383
384 void
385 kmem_free(void *p, size_t size)
386 {
387 KASSERT(!cpu_intr_p());
388 KASSERT(!cpu_softintr_p());
389 kmem_intr_free(p, size);
390 }
391
392 static size_t
393 kmem_create_caches(const struct kmem_cache_info *array,
394 pool_cache_t alloc_table[], size_t maxsize, int shift, int ipl)
395 {
396 size_t maxidx = 0;
397 size_t table_unit = (1 << shift);
398 size_t size = table_unit;
399 int i;
400
401 for (i = 0; array[i].kc_size != 0 ; i++) {
402 const char *name = array[i].kc_name;
403 size_t cache_size = array[i].kc_size;
404 struct pool_allocator *pa;
405 int flags = PR_NOALIGN;
406 pool_cache_t pc;
407 size_t align;
408
409 if ((cache_size & (CACHE_LINE_SIZE - 1)) == 0)
410 align = CACHE_LINE_SIZE;
411 else if ((cache_size & (PAGE_SIZE - 1)) == 0)
412 align = PAGE_SIZE;
413 else
414 align = KMEM_ALIGN;
415
416 if (cache_size < CACHE_LINE_SIZE)
417 flags |= PR_NOTOUCH;
418
419 /* check if we reached the requested size */
420 if (cache_size > maxsize || cache_size > PAGE_SIZE) {
421 break;
422 }
423 if ((cache_size >> shift) > maxidx) {
424 maxidx = cache_size >> shift;
425 }
426
427 if ((cache_size >> shift) > maxidx) {
428 maxidx = cache_size >> shift;
429 }
430
431 pa = &pool_allocator_kmem;
432 pc = pool_cache_init(cache_size, align, 0, flags,
433 name, pa, ipl, NULL, NULL, NULL);
434
435 while (size <= cache_size) {
436 alloc_table[(size - 1) >> shift] = pc;
437 size += table_unit;
438 }
439 }
440 return maxidx;
441 }
442
443 void
444 kmem_init(void)
445 {
446 #ifdef KMEM_GUARD
447 kmem_guard_enabled = kmem_guard_init(&kmem_guard, kmem_guard_depth,
448 kmem_va_arena);
449 #endif
450 kmem_cache_maxidx = kmem_create_caches(kmem_cache_sizes,
451 kmem_cache, KMEM_MAXSIZE, KMEM_SHIFT, IPL_VM);
452 kmem_cache_big_maxidx = kmem_create_caches(kmem_cache_big_sizes,
453 kmem_cache_big, PAGE_SIZE, KMEM_BIG_SHIFT, IPL_VM);
454 }
455
456 size_t
457 kmem_roundup_size(size_t size)
458 {
459 return (size + (KMEM_ALIGN - 1)) & ~(KMEM_ALIGN - 1);
460 }
461
462 /*
463 * Used to dynamically allocate string with kmem accordingly to format.
464 */
465 char *
466 kmem_asprintf(const char *fmt, ...)
467 {
468 int size __diagused, len;
469 va_list va;
470 char *str;
471
472 va_start(va, fmt);
473 len = vsnprintf(NULL, 0, fmt, va);
474 va_end(va);
475
476 str = kmem_alloc(len + 1, KM_SLEEP);
477
478 va_start(va, fmt);
479 size = vsnprintf(str, len + 1, fmt, va);
480 va_end(va);
481
482 KASSERT(size == len);
483
484 return str;
485 }
486
487 char *
488 kmem_strdupsize(const char *str, size_t *lenp, km_flag_t flags)
489 {
490 size_t len = strlen(str) + 1;
491 char *ptr = kmem_alloc(len, flags);
492 if (ptr == NULL)
493 return NULL;
494
495 if (lenp)
496 *lenp = len;
497 memcpy(ptr, str, len);
498 return ptr;
499 }
500
501 char *
502 kmem_strndup(const char *str, size_t maxlen, km_flag_t flags)
503 {
504 KASSERT(str != NULL);
505 KASSERT(maxlen != 0);
506
507 size_t len = strnlen(str, maxlen);
508 char *ptr = kmem_alloc(len + 1, flags);
509 if (ptr == NULL)
510 return NULL;
511
512 memcpy(ptr, str, len);
513 ptr[len] = '\0';
514
515 return ptr;
516 }
517
518 void
519 kmem_strfree(char *str)
520 {
521 if (str == NULL)
522 return;
523
524 kmem_free(str, strlen(str) + 1);
525 }
526
527 /* ------------------ DEBUG / DIAGNOSTIC ------------------ */
528
529 #if defined(KMEM_SIZE)
530 static void
531 kmem_size_set(void *p, size_t sz)
532 {
533 struct kmem_header *hd;
534 hd = (struct kmem_header *)p;
535 hd->size = sz;
536 }
537
538 static void
539 kmem_size_check(void *p, size_t sz)
540 {
541 struct kmem_header *hd;
542 size_t hsz;
543
544 hd = (struct kmem_header *)p;
545 hsz = hd->size;
546
547 if (hsz != sz) {
548 panic("kmem_free(%p, %zu) != allocated size %zu",
549 (const uint8_t *)p + SIZE_SIZE, sz, hsz);
550 }
551 }
552 #endif /* defined(KMEM_SIZE) */
553
554 #if defined(KMEM_GUARD)
555 /*
556 * The ultimate memory allocator for debugging, baby. It tries to catch:
557 *
558 * 1. Overflow, in realtime. A guard page sits immediately after the
559 * requested area; a read/write overflow therefore triggers a page
560 * fault.
561 * 2. Invalid pointer/size passed, at free. A kmem_header structure sits
562 * just before the requested area, and holds the allocated size. Any
563 * difference with what is given at free triggers a panic.
564 * 3. Underflow, at free. If an underflow occurs, the kmem header will be
565 * modified, and 2. will trigger a panic.
566 * 4. Use-after-free. When freeing, the memory is unmapped, and depending
567 * on the value of kmem_guard_depth, the kernel will more or less delay
568 * the recycling of that memory. Which means that any ulterior read/write
569 * access to the memory will trigger a page fault, given it hasn't been
570 * recycled yet.
571 */
572
573 #include <sys/atomic.h>
574 #include <uvm/uvm.h>
575
576 static bool
577 kmem_guard_init(struct kmem_guard *kg, u_int depth, vmem_t *vm)
578 {
579 vaddr_t va;
580
581 /* If not enabled, we have nothing to do. */
582 if (depth == 0) {
583 return false;
584 }
585 depth = roundup(depth, PAGE_SIZE / sizeof(void *));
586 KASSERT(depth != 0);
587
588 /*
589 * Allocate fifo.
590 */
591 va = uvm_km_alloc(kernel_map, depth * sizeof(void *), PAGE_SIZE,
592 UVM_KMF_WIRED | UVM_KMF_ZERO);
593 if (va == 0) {
594 return false;
595 }
596
597 /*
598 * Init object.
599 */
600 kg->kg_vmem = vm;
601 kg->kg_fifo = (void *)va;
602 kg->kg_depth = depth;
603 kg->kg_rotor = 0;
604
605 printf("kmem_guard(%p): depth %d\n", kg, depth);
606 return true;
607 }
608
609 static void *
610 kmem_guard_alloc(struct kmem_guard *kg, size_t requested_size, bool waitok)
611 {
612 struct vm_page *pg;
613 vm_flag_t flags;
614 vmem_addr_t va;
615 vaddr_t loopva;
616 vsize_t loopsize;
617 size_t size;
618 void **p;
619
620 /*
621 * Compute the size: take the kmem header into account, and add a guard
622 * page at the end.
623 */
624 size = round_page(requested_size + SIZE_SIZE) + PAGE_SIZE;
625
626 /* Allocate pages of kernel VA, but do not map anything in yet. */
627 flags = VM_BESTFIT | (waitok ? VM_SLEEP : VM_NOSLEEP);
628 if (vmem_alloc(kg->kg_vmem, size, flags, &va) != 0) {
629 return NULL;
630 }
631
632 loopva = va;
633 loopsize = size - PAGE_SIZE;
634
635 while (loopsize) {
636 pg = uvm_pagealloc(NULL, loopva, NULL, 0);
637 if (__predict_false(pg == NULL)) {
638 if (waitok) {
639 uvm_wait("kmem_guard");
640 continue;
641 } else {
642 uvm_km_pgremove_intrsafe(kernel_map, va,
643 va + size);
644 vmem_free(kg->kg_vmem, va, size);
645 return NULL;
646 }
647 }
648
649 pg->flags &= ~PG_BUSY; /* new page */
650 UVM_PAGE_OWN(pg, NULL);
651 pmap_kenter_pa(loopva, VM_PAGE_TO_PHYS(pg),
652 VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
653
654 loopva += PAGE_SIZE;
655 loopsize -= PAGE_SIZE;
656 }
657
658 pmap_update(pmap_kernel());
659
660 /*
661 * Offset the returned pointer so that the unmapped guard page sits
662 * immediately after the returned object.
663 */
664 p = (void **)((va + (size - PAGE_SIZE) - requested_size) & ~(uintptr_t)ALIGNBYTES);
665 kmem_size_set((uint8_t *)p - SIZE_SIZE, requested_size);
666 return (void *)p;
667 }
668
669 static void
670 kmem_guard_free(struct kmem_guard *kg, size_t requested_size, void *p)
671 {
672 vaddr_t va;
673 u_int rotor;
674 size_t size;
675 uint8_t *ptr;
676
677 ptr = (uint8_t *)p - SIZE_SIZE;
678 kmem_size_check(ptr, requested_size);
679 va = trunc_page((vaddr_t)ptr);
680 size = round_page(requested_size + SIZE_SIZE) + PAGE_SIZE;
681
682 KASSERT(pmap_extract(pmap_kernel(), va, NULL));
683 KASSERT(!pmap_extract(pmap_kernel(), va + (size - PAGE_SIZE), NULL));
684
685 /*
686 * Unmap and free the pages. The last one is never allocated.
687 */
688 uvm_km_pgremove_intrsafe(kernel_map, va, va + size);
689 pmap_update(pmap_kernel());
690
691 #if 0
692 /*
693 * XXX: Here, we need to atomically register the va and its size in the
694 * fifo.
695 */
696
697 /*
698 * Put the VA allocation into the list and swap an old one out to free.
699 * This behaves mostly like a fifo.
700 */
701 rotor = atomic_inc_uint_nv(&kg->kg_rotor) % kg->kg_depth;
702 va = (vaddr_t)atomic_swap_ptr(&kg->kg_fifo[rotor], (void *)va);
703 if (va != 0) {
704 vmem_free(kg->kg_vmem, va, size);
705 }
706 #else
707 (void)rotor;
708 vmem_free(kg->kg_vmem, va, size);
709 #endif
710 }
711
712 #endif /* defined(KMEM_GUARD) */
713