dmapool: make DMAPOOL_DEBUG detect corruption of free marker
authorMatthieu CASTET <matthieu.castet@parrot.com>
Wed, 12 Dec 2012 00:01:31 +0000 (16:01 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 12 Dec 2012 01:22:24 +0000 (17:22 -0800)
This can help to catch the case where hardware is writing after dma free.

[akpm@linux-foundation.org: tidy code, fix comment, use sizeof(page->offset), use pr_err()]
Signed-off-by: Matthieu Castet <matthieu.castet@parrot.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
mm/dmapool.c

index da1b0f0b8709d1eaf485fcd8bd8c86c0e69db6d6..c69781e97cf968076583cc29ef1dbfa53a535c46 100644 (file)
@@ -332,6 +332,30 @@ void *dma_pool_alloc(struct dma_pool *pool, gfp_t mem_flags,
        retval = offset + page->vaddr;
        *handle = offset + page->dma;
 #ifdef DMAPOOL_DEBUG
+       {
+               int i;
+               u8 *data = retval;
+               /* page->offset is stored in first 4 bytes */
+               for (i = sizeof(page->offset); i < pool->size; i++) {
+                       if (data[i] == POOL_POISON_FREED)
+                               continue;
+                       if (pool->dev)
+                               dev_err(pool->dev,
+                                       "dma_pool_alloc %s, %p (corruped)\n",
+                                       pool->name, retval);
+                       else
+                               pr_err("dma_pool_alloc %s, %p (corruped)\n",
+                                       pool->name, retval);
+
+                       /*
+                        * Dump the first 4 bytes even if they are not
+                        * POOL_POISON_FREED
+                        */
+                       print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1,
+                                       data, pool->size, 1);
+                       break;
+               }
+       }
        memset(retval, POOL_POISON_ALLOCATED, pool->size);
 #endif
        spin_unlock_irqrestore(&pool->lock, flags);