The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/vm/uma_dbg.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
    3  * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
    4  * All rights reserved.
    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>
   34 __FBSDID("$FreeBSD: releng/10.4/sys/vm/uma_dbg.c 252040 2013-06-20 19:08:12Z jeff $");
   35 
   36 #include <sys/param.h>
   37 #include <sys/systm.h>
   38 #include <sys/bitset.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 
   53 static const uint32_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  */
   62 int
   63 trash_ctor(void *mem, int size, void *arg, int flags)
   64 {
   65         int cnt;
   66         uint32_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  */
   85 void
   86 trash_dtor(void *mem, int size, void *arg)
   87 {
   88         int cnt;
   89         uint32_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  */
  103 int
  104 trash_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  */
  116 void
  117 trash_fini(void *mem, int size)
  118 {
  119         (void)trash_ctor(mem, size, NULL, 0);
  120 }
  121 
  122 int
  123 mtrash_ctor(void *mem, int size, void *arg, int flags)
  124 {
  125         struct malloc_type **ksp;
  126         uint32_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  */
  150 void
  151 mtrash_dtor(void *mem, int size, void *arg)
  152 {
  153         int cnt;
  154         uint32_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  */
  169 int
  170 mtrash_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  */
  189 void
  190 mtrash_fini(void *mem, int size)
  191 {
  192         (void)mtrash_ctor(mem, size, NULL, 0);
  193 }
  194 
  195 #ifdef INVARIANTS
  196 static uma_slab_t
  197 uma_dbg_getslab(uma_zone_t zone, void *item)
  198 {
  199         uma_slab_t slab;
  200         uma_keg_t keg;
  201         uint8_t *mem;
  202 
  203         mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
  204         if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
  205                 slab = vtoslab((vm_offset_t)mem);
  206         } else {
  207                 /*
  208                  * It is safe to return the slab here even though the
  209                  * zone is unlocked because the item's allocation state
  210                  * essentially holds a reference.
  211                  */
  212                 ZONE_LOCK(zone);
  213                 keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
  214                 if (keg->uk_flags & UMA_ZONE_HASH)
  215                         slab = hash_sfind(&keg->uk_hash, mem);
  216                 else
  217                         slab = (uma_slab_t)(mem + keg->uk_pgoff);
  218                 ZONE_UNLOCK(zone);
  219         }
  220 
  221         return (slab);
  222 }
  223 
  224 /*
  225  * Set up the slab's freei data such that uma_dbg_free can function.
  226  *
  227  */
  228 void
  229 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
  230 {
  231         uma_keg_t keg;
  232         int freei;
  233 
  234         if (zone_first_keg(zone) == NULL)
  235                 return;
  236         if (slab == NULL) {
  237                 slab = uma_dbg_getslab(zone, item);
  238                 if (slab == NULL) 
  239                         panic("uma: item %p did not belong to zone %s\n",
  240                             item, zone->uz_name);
  241         }
  242         keg = slab->us_keg;
  243         freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
  244 
  245         if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
  246                 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
  247                     item, zone, zone->uz_name, slab, freei);
  248         BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
  249 
  250         return;
  251 }
  252 
  253 /*
  254  * Verifies freed addresses.  Checks for alignment, valid slab membership
  255  * and duplicate frees.
  256  *
  257  */
  258 void
  259 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
  260 {
  261         uma_keg_t keg;
  262         int freei;
  263 
  264         if (zone_first_keg(zone) == NULL)
  265                 return;
  266         if (slab == NULL) {
  267                 slab = uma_dbg_getslab(zone, item);
  268                 if (slab == NULL) 
  269                         panic("uma: Freed item %p did not belong to zone %s\n",
  270                             item, zone->uz_name);
  271         }
  272         keg = slab->us_keg;
  273         freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
  274 
  275         if (freei >= keg->uk_ipers)
  276                 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
  277                     item, zone, zone->uz_name, slab, freei);
  278 
  279         if (((freei * keg->uk_rsize) + slab->us_data) != item) 
  280                 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
  281                     item, zone, zone->uz_name, slab, freei);
  282 
  283         if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
  284                 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
  285                     item, zone, zone->uz_name, slab, freei);
  286 
  287         BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
  288 }
  289 
  290 #endif /* INVARIANTS */

Cache object: 901982bba29f18a4a3bc7d6d0a421933


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.