2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
5 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * uma_dbg.c Debugging features for UMA users
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bitset.h>
43 #include <sys/kernel.h>
44 #include <sys/types.h>
45 #include <sys/queue.h>
47 #include <sys/mutex.h>
48 #include <sys/malloc.h>
51 #include <vm/vm_object.h>
52 #include <vm/vm_page.h>
54 #include <vm/uma_int.h>
55 #include <vm/uma_dbg.h>
56 #include <vm/memguard.h>
58 static const uint32_t uma_junk = 0xdeadc0de;
61 * Checks an item to make sure it hasn't been overwritten since it was freed,
62 * prior to subsequent reallocation.
64 * Complies with standard ctor arg/return
67 trash_ctor(void *mem, int size, void *arg, int flags)
73 if (is_memguard_addr(mem))
77 cnt = size / sizeof(uma_junk);
79 for (p = mem; cnt > 0; cnt--, p++)
82 panic("Memory modified after free %p(%d) val=%x @ %p\n",
85 printf("Memory modified after free %p(%d) val=%x @ %p\n",
94 * Fills an item with predictable garbage
96 * Complies with standard dtor arg/return
100 trash_dtor(void *mem, int size, void *arg)
105 #ifdef DEBUG_MEMGUARD
106 if (is_memguard_addr(mem))
110 cnt = size / sizeof(uma_junk);
112 for (p = mem; cnt > 0; cnt--, p++)
117 * Fills an item with predictable garbage
119 * Complies with standard init arg/return
123 trash_init(void *mem, int size, int flags)
125 trash_dtor(mem, size, NULL);
130 * Checks an item to make sure it hasn't been overwritten since it was freed.
132 * Complies with standard fini arg/return
136 trash_fini(void *mem, int size)
138 (void)trash_ctor(mem, size, NULL, 0);
142 mtrash_ctor(void *mem, int size, void *arg, int flags)
144 struct malloc_type **ksp;
148 #ifdef DEBUG_MEMGUARD
149 if (is_memguard_addr(mem))
153 size -= sizeof(struct malloc_type *);
154 ksp = (struct malloc_type **)mem;
155 ksp += size / sizeof(struct malloc_type *);
156 cnt = size / sizeof(uma_junk);
158 for (p = mem; cnt > 0; cnt--, p++)
159 if (*p != uma_junk) {
160 printf("Memory modified after free %p(%d) val=%x @ %p\n",
162 panic("Most recently used by %s\n", (*ksp == NULL)?
163 "none" : (*ksp)->ks_shortdesc);
169 * Fills an item with predictable garbage
171 * Complies with standard dtor arg/return
175 mtrash_dtor(void *mem, int size, void *arg)
180 #ifdef DEBUG_MEMGUARD
181 if (is_memguard_addr(mem))
185 size -= sizeof(struct malloc_type *);
186 cnt = size / sizeof(uma_junk);
188 for (p = mem; cnt > 0; cnt--, p++)
193 * Fills an item with predictable garbage
195 * Complies with standard init arg/return
199 mtrash_init(void *mem, int size, int flags)
201 struct malloc_type **ksp;
203 #ifdef DEBUG_MEMGUARD
204 if (is_memguard_addr(mem))
208 mtrash_dtor(mem, size, NULL);
210 ksp = (struct malloc_type **)mem;
211 ksp += (size / sizeof(struct malloc_type *)) - 1;
217 * Checks an item to make sure it hasn't been overwritten since it was freed,
218 * prior to freeing it back to available memory.
220 * Complies with standard fini arg/return
224 mtrash_fini(void *mem, int size)
226 (void)mtrash_ctor(mem, size, NULL, 0);