6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * uma_dbg.c Debugging features for UMA users 31 * 32 */ 33 34#include <sys/cdefs.h>
| 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice unmodified, this list of conditions, and the following 11 * disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28/* 29 * uma_dbg.c Debugging features for UMA users 30 * 31 */ 32 33#include <sys/cdefs.h>
|
36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/types.h> 41#include <sys/queue.h> 42#include <sys/lock.h> 43#include <sys/mutex.h> 44#include <sys/malloc.h> 45 46#include <vm/vm.h> 47#include <vm/vm_object.h> 48#include <vm/vm_page.h> 49#include <vm/uma.h> 50#include <vm/uma_int.h> 51#include <vm/uma_dbg.h> 52 53static const u_int32_t uma_junk = 0xdeadc0de; 54 55/* 56 * Checks an item to make sure it hasn't been overwritten since it was freed, 57 * prior to subsequent reallocation. 58 * 59 * Complies with standard ctor arg/return 60 * 61 */ 62int 63trash_ctor(void *mem, int size, void *arg, int flags) 64{ 65 int cnt; 66 u_int32_t *p; 67 68 cnt = size / sizeof(uma_junk); 69 70 for (p = mem; cnt > 0; cnt--, p++) 71 if (*p != uma_junk) { 72 printf("Memory modified after free %p(%d) val=%x @ %p\n", 73 mem, size, *p, p); 74 return (0); 75 } 76 return (0); 77} 78 79/* 80 * Fills an item with predictable garbage 81 * 82 * Complies with standard dtor arg/return 83 * 84 */ 85void 86trash_dtor(void *mem, int size, void *arg) 87{ 88 int cnt; 89 u_int32_t *p; 90 91 cnt = size / sizeof(uma_junk); 92 93 for (p = mem; cnt > 0; cnt--, p++) 94 *p = uma_junk; 95} 96 97/* 98 * Fills an item with predictable garbage 99 * 100 * Complies with standard init arg/return 101 * 102 */ 103int 104trash_init(void *mem, int size, int flags) 105{ 106 trash_dtor(mem, size, NULL); 107 return (0); 108} 109 110/* 111 * Checks an item to make sure it hasn't been overwritten since it was freed. 112 * 113 * Complies with standard fini arg/return 114 * 115 */ 116void 117trash_fini(void *mem, int size) 118{ 119 (void)trash_ctor(mem, size, NULL, 0); 120} 121 122int 123mtrash_ctor(void *mem, int size, void *arg, int flags) 124{ 125 struct malloc_type **ksp; 126 u_int32_t *p = mem; 127 int cnt; 128 129 size -= sizeof(struct malloc_type *); 130 ksp = (struct malloc_type **)mem; 131 ksp += size / sizeof(struct malloc_type *); 132 cnt = size / sizeof(uma_junk); 133 134 for (p = mem; cnt > 0; cnt--, p++) 135 if (*p != uma_junk) { 136 printf("Memory modified after free %p(%d) val=%x @ %p\n", 137 mem, size, *p, p); 138 panic("Most recently used by %s\n", (*ksp == NULL)? 139 "none" : (*ksp)->ks_shortdesc); 140 } 141 return (0); 142} 143 144/* 145 * Fills an item with predictable garbage 146 * 147 * Complies with standard dtor arg/return 148 * 149 */ 150void 151mtrash_dtor(void *mem, int size, void *arg) 152{ 153 int cnt; 154 u_int32_t *p; 155 156 size -= sizeof(struct malloc_type *); 157 cnt = size / sizeof(uma_junk); 158 159 for (p = mem; cnt > 0; cnt--, p++) 160 *p = uma_junk; 161} 162 163/* 164 * Fills an item with predictable garbage 165 * 166 * Complies with standard init arg/return 167 * 168 */ 169int 170mtrash_init(void *mem, int size, int flags) 171{ 172 struct malloc_type **ksp; 173 174 mtrash_dtor(mem, size, NULL); 175 176 ksp = (struct malloc_type **)mem; 177 ksp += (size / sizeof(struct malloc_type *)) - 1; 178 *ksp = NULL; 179 return (0); 180} 181 182/* 183 * Checks an item to make sure it hasn't been overwritten since it was freed, 184 * prior to freeing it back to available memory. 185 * 186 * Complies with standard fini arg/return 187 * 188 */ 189void 190mtrash_fini(void *mem, int size) 191{ 192 (void)mtrash_ctor(mem, size, NULL, 0); 193} 194 195static uma_slab_t 196uma_dbg_getslab(uma_zone_t zone, void *item) 197{ 198 uma_slab_t slab; 199 uma_keg_t keg; 200 u_int8_t *mem; 201 202 keg = zone->uz_keg; 203 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK)); 204 if (keg->uk_flags & UMA_ZONE_MALLOC) { 205 slab = vtoslab((vm_offset_t)mem); 206 } else if (keg->uk_flags & UMA_ZONE_HASH) { 207 slab = hash_sfind(&keg->uk_hash, mem); 208 } else { 209 mem += keg->uk_pgoff; 210 slab = (uma_slab_t)mem; 211 } 212 213 return (slab); 214} 215 216/* 217 * Set up the slab's freei data such that uma_dbg_free can function. 218 * 219 */ 220 221void 222uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 223{ 224 uma_keg_t keg; 225 uma_slabrefcnt_t slabref; 226 int freei; 227 228 keg = zone->uz_keg; 229 if (slab == NULL) { 230 slab = uma_dbg_getslab(zone, item); 231 if (slab == NULL) 232 panic("uma: item %p did not belong to zone %s\n", 233 item, zone->uz_name); 234 } 235 236 freei = ((unsigned long)item - (unsigned long)slab->us_data) 237 / keg->uk_rsize; 238 239 if (keg->uk_flags & UMA_ZONE_REFCNT) { 240 slabref = (uma_slabrefcnt_t)slab; 241 slabref->us_freelist[freei].us_item = 255; 242 } else { 243 slab->us_freelist[freei].us_item = 255; 244 } 245 246 return; 247} 248 249/* 250 * Verifies freed addresses. Checks for alignment, valid slab membership 251 * and duplicate frees. 252 * 253 */ 254 255void 256uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 257{ 258 uma_keg_t keg; 259 uma_slabrefcnt_t slabref; 260 int freei; 261 262 keg = zone->uz_keg; 263 if (slab == NULL) { 264 slab = uma_dbg_getslab(zone, item); 265 if (slab == NULL) 266 panic("uma: Freed item %p did not belong to zone %s\n", 267 item, zone->uz_name); 268 } 269 270 freei = ((unsigned long)item - (unsigned long)slab->us_data) 271 / keg->uk_rsize; 272 273 if (freei >= keg->uk_ipers) 274 panic("zone: %s(%p) slab %p freelist %d out of range 0-%d\n", 275 zone->uz_name, zone, slab, freei, keg->uk_ipers-1); 276 277 if (((freei * keg->uk_rsize) + slab->us_data) != item) { 278 printf("zone: %s(%p) slab %p freed address %p unaligned.\n", 279 zone->uz_name, zone, slab, item); 280 panic("should be %p\n", 281 (freei * keg->uk_rsize) + slab->us_data); 282 } 283 284 if (keg->uk_flags & UMA_ZONE_REFCNT) { 285 slabref = (uma_slabrefcnt_t)slab; 286 if (slabref->us_freelist[freei].us_item != 255) { 287 printf("Slab at %p, freei %d = %d.\n", 288 slab, freei, slabref->us_freelist[freei].us_item); 289 panic("Duplicate free of item %p from zone %p(%s)\n", 290 item, zone, zone->uz_name); 291 } 292 293 /* 294 * When this is actually linked into the slab this will change. 295 * Until then the count of valid slabs will make sure we don't 296 * accidentally follow this and assume it's a valid index. 297 */ 298 slabref->us_freelist[freei].us_item = 0; 299 } else { 300 if (slab->us_freelist[freei].us_item != 255) { 301 printf("Slab at %p, freei %d = %d.\n", 302 slab, freei, slab->us_freelist[freei].us_item); 303 panic("Duplicate free of item %p from zone %p(%s)\n", 304 item, zone, zone->uz_name); 305 } 306 307 /* 308 * When this is actually linked into the slab this will change. 309 * Until then the count of valid slabs will make sure we don't 310 * accidentally follow this and assume it's a valid index. 311 */ 312 slab->us_freelist[freei].us_item = 0; 313 } 314}
| 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/kernel.h> 39#include <sys/types.h> 40#include <sys/queue.h> 41#include <sys/lock.h> 42#include <sys/mutex.h> 43#include <sys/malloc.h> 44 45#include <vm/vm.h> 46#include <vm/vm_object.h> 47#include <vm/vm_page.h> 48#include <vm/uma.h> 49#include <vm/uma_int.h> 50#include <vm/uma_dbg.h> 51 52static const u_int32_t uma_junk = 0xdeadc0de; 53 54/* 55 * Checks an item to make sure it hasn't been overwritten since it was freed, 56 * prior to subsequent reallocation. 57 * 58 * Complies with standard ctor arg/return 59 * 60 */ 61int 62trash_ctor(void *mem, int size, void *arg, int flags) 63{ 64 int cnt; 65 u_int32_t *p; 66 67 cnt = size / sizeof(uma_junk); 68 69 for (p = mem; cnt > 0; cnt--, p++) 70 if (*p != uma_junk) { 71 printf("Memory modified after free %p(%d) val=%x @ %p\n", 72 mem, size, *p, p); 73 return (0); 74 } 75 return (0); 76} 77 78/* 79 * Fills an item with predictable garbage 80 * 81 * Complies with standard dtor arg/return 82 * 83 */ 84void 85trash_dtor(void *mem, int size, void *arg) 86{ 87 int cnt; 88 u_int32_t *p; 89 90 cnt = size / sizeof(uma_junk); 91 92 for (p = mem; cnt > 0; cnt--, p++) 93 *p = uma_junk; 94} 95 96/* 97 * Fills an item with predictable garbage 98 * 99 * Complies with standard init arg/return 100 * 101 */ 102int 103trash_init(void *mem, int size, int flags) 104{ 105 trash_dtor(mem, size, NULL); 106 return (0); 107} 108 109/* 110 * Checks an item to make sure it hasn't been overwritten since it was freed. 111 * 112 * Complies with standard fini arg/return 113 * 114 */ 115void 116trash_fini(void *mem, int size) 117{ 118 (void)trash_ctor(mem, size, NULL, 0); 119} 120 121int 122mtrash_ctor(void *mem, int size, void *arg, int flags) 123{ 124 struct malloc_type **ksp; 125 u_int32_t *p = mem; 126 int cnt; 127 128 size -= sizeof(struct malloc_type *); 129 ksp = (struct malloc_type **)mem; 130 ksp += size / sizeof(struct malloc_type *); 131 cnt = size / sizeof(uma_junk); 132 133 for (p = mem; cnt > 0; cnt--, p++) 134 if (*p != uma_junk) { 135 printf("Memory modified after free %p(%d) val=%x @ %p\n", 136 mem, size, *p, p); 137 panic("Most recently used by %s\n", (*ksp == NULL)? 138 "none" : (*ksp)->ks_shortdesc); 139 } 140 return (0); 141} 142 143/* 144 * Fills an item with predictable garbage 145 * 146 * Complies with standard dtor arg/return 147 * 148 */ 149void 150mtrash_dtor(void *mem, int size, void *arg) 151{ 152 int cnt; 153 u_int32_t *p; 154 155 size -= sizeof(struct malloc_type *); 156 cnt = size / sizeof(uma_junk); 157 158 for (p = mem; cnt > 0; cnt--, p++) 159 *p = uma_junk; 160} 161 162/* 163 * Fills an item with predictable garbage 164 * 165 * Complies with standard init arg/return 166 * 167 */ 168int 169mtrash_init(void *mem, int size, int flags) 170{ 171 struct malloc_type **ksp; 172 173 mtrash_dtor(mem, size, NULL); 174 175 ksp = (struct malloc_type **)mem; 176 ksp += (size / sizeof(struct malloc_type *)) - 1; 177 *ksp = NULL; 178 return (0); 179} 180 181/* 182 * Checks an item to make sure it hasn't been overwritten since it was freed, 183 * prior to freeing it back to available memory. 184 * 185 * Complies with standard fini arg/return 186 * 187 */ 188void 189mtrash_fini(void *mem, int size) 190{ 191 (void)mtrash_ctor(mem, size, NULL, 0); 192} 193 194static uma_slab_t 195uma_dbg_getslab(uma_zone_t zone, void *item) 196{ 197 uma_slab_t slab; 198 uma_keg_t keg; 199 u_int8_t *mem; 200 201 keg = zone->uz_keg; 202 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK)); 203 if (keg->uk_flags & UMA_ZONE_MALLOC) { 204 slab = vtoslab((vm_offset_t)mem); 205 } else if (keg->uk_flags & UMA_ZONE_HASH) { 206 slab = hash_sfind(&keg->uk_hash, mem); 207 } else { 208 mem += keg->uk_pgoff; 209 slab = (uma_slab_t)mem; 210 } 211 212 return (slab); 213} 214 215/* 216 * Set up the slab's freei data such that uma_dbg_free can function. 217 * 218 */ 219 220void 221uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 222{ 223 uma_keg_t keg; 224 uma_slabrefcnt_t slabref; 225 int freei; 226 227 keg = zone->uz_keg; 228 if (slab == NULL) { 229 slab = uma_dbg_getslab(zone, item); 230 if (slab == NULL) 231 panic("uma: item %p did not belong to zone %s\n", 232 item, zone->uz_name); 233 } 234 235 freei = ((unsigned long)item - (unsigned long)slab->us_data) 236 / keg->uk_rsize; 237 238 if (keg->uk_flags & UMA_ZONE_REFCNT) { 239 slabref = (uma_slabrefcnt_t)slab; 240 slabref->us_freelist[freei].us_item = 255; 241 } else { 242 slab->us_freelist[freei].us_item = 255; 243 } 244 245 return; 246} 247 248/* 249 * Verifies freed addresses. Checks for alignment, valid slab membership 250 * and duplicate frees. 251 * 252 */ 253 254void 255uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 256{ 257 uma_keg_t keg; 258 uma_slabrefcnt_t slabref; 259 int freei; 260 261 keg = zone->uz_keg; 262 if (slab == NULL) { 263 slab = uma_dbg_getslab(zone, item); 264 if (slab == NULL) 265 panic("uma: Freed item %p did not belong to zone %s\n", 266 item, zone->uz_name); 267 } 268 269 freei = ((unsigned long)item - (unsigned long)slab->us_data) 270 / keg->uk_rsize; 271 272 if (freei >= keg->uk_ipers) 273 panic("zone: %s(%p) slab %p freelist %d out of range 0-%d\n", 274 zone->uz_name, zone, slab, freei, keg->uk_ipers-1); 275 276 if (((freei * keg->uk_rsize) + slab->us_data) != item) { 277 printf("zone: %s(%p) slab %p freed address %p unaligned.\n", 278 zone->uz_name, zone, slab, item); 279 panic("should be %p\n", 280 (freei * keg->uk_rsize) + slab->us_data); 281 } 282 283 if (keg->uk_flags & UMA_ZONE_REFCNT) { 284 slabref = (uma_slabrefcnt_t)slab; 285 if (slabref->us_freelist[freei].us_item != 255) { 286 printf("Slab at %p, freei %d = %d.\n", 287 slab, freei, slabref->us_freelist[freei].us_item); 288 panic("Duplicate free of item %p from zone %p(%s)\n", 289 item, zone, zone->uz_name); 290 } 291 292 /* 293 * When this is actually linked into the slab this will change. 294 * Until then the count of valid slabs will make sure we don't 295 * accidentally follow this and assume it's a valid index. 296 */ 297 slabref->us_freelist[freei].us_item = 0; 298 } else { 299 if (slab->us_freelist[freei].us_item != 255) { 300 printf("Slab at %p, freei %d = %d.\n", 301 slab, freei, slab->us_freelist[freei].us_item); 302 panic("Duplicate free of item %p from zone %p(%s)\n", 303 item, zone, zone->uz_name); 304 } 305 306 /* 307 * When this is actually linked into the slab this will change. 308 * Until then the count of valid slabs will make sure we don't 309 * accidentally follow this and assume it's a valid index. 310 */ 311 slab->us_freelist[freei].us_item = 0; 312 } 313}
|