mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joerg Roedel <joerg.roedel@amd.com>
To: Ingo Molnar <mingo@redhat.com>, Thomas Gleixner <tglx@linutronix.de>
Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org,
	iommu@lists.linux-foundation.org,
	Joerg Roedel <joerg.roedel@amd.com>
Subject: [PATCH 04/10] x86: add helper functions for consistency checks
Date: Fri, 21 Nov 2008 17:26:04 +0100	[thread overview]
Message-ID: <1227284770-19215-5-git-send-email-joerg.roedel@amd.com> (raw)
In-Reply-To: <1227284770-19215-1-git-send-email-joerg.roedel@amd.com>

Impact: adds helper functions to be used later

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
---
 arch/x86/kernel/pci-dma-debug.c |  125 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 125 insertions(+), 0 deletions(-)

diff --git a/arch/x86/kernel/pci-dma-debug.c b/arch/x86/kernel/pci-dma-debug.c
index c2d3408..fc95631 100644
--- a/arch/x86/kernel/pci-dma-debug.c
+++ b/arch/x86/kernel/pci-dma-debug.c
@@ -42,6 +42,11 @@ static struct kmem_cache *dma_entry_cache;
 /* lock to protect the data structures */
 static DEFINE_SPINLOCK(dma_lock);
 
+static char *type2name[3] = { "single", "scather-gather", "coherent" };
+
+static char *dir2name[4] = { "DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
+			     "DMA_FROM_DEVICE", "DMA_NONE" };
+
 static int hash_fn(struct dma_debug_entry *entry)
 {
 	/*
@@ -95,6 +100,126 @@ static void remove_dma_entry(struct dma_debug_entry *entry)
 	list_del(&entry->list);
 }
 
+static bool check_unmap(struct dma_debug_entry *ref,
+		struct dma_debug_entry *entry)
+{
+	bool errors = false;
+
+	if (!entry) {
+		dev_printk(KERN_ERR, ref->dev, "PCI-DMA: device driver tries "
+			   "to free DMA memory it has not allocated "
+			   "[device address=0x%016llx] [size=%llu bytes]\n",
+			   ref->dev_addr, ref->size);
+		dump_stack();
+
+		return false;
+	}
+
+	if (ref->size != entry->size) {
+		dev_printk(KERN_ERR, ref->dev, "PCI-DMA: device driver frees "
+			   "DMA memory with different size "
+			   "[device address=0x%016llx] [map size=%llu bytes] "
+			   "[unmap size=%llu bytes]\n",
+			   ref->dev_addr, entry->size, ref->size);
+		errors = true;
+	}
+
+	if (ref->type != entry->type) {
+		dev_printk(KERN_ERR, ref->dev, "PCI-DMA: device driver frees "
+			   "DMA memory different that it was allocated "
+			   "[device address=0x%016llx] [size=%llu bytes] "
+			   "[mapped as %s] [unmapped as %s]\n",
+			   ref->dev_addr, ref->size,
+			   type2name[entry->type], type2name[ref->type]);
+		errors = true;
+	} else if ((entry->type == DMA_DEBUG_COHERENT) &&
+		   (ref->cpu_addr != entry->cpu_addr)) {
+		dev_printk(KERN_ERR, ref->dev, "PCI-DMA: device driver frees "
+			   "DMA memory with different CPU address "
+			   "[device address=0x%016llx] [size=%llu bytes] "
+			   "[cpu alloc address=%p] [cpu free address=%p]",
+			   ref->dev_addr, ref->size,
+			   entry->cpu_addr, ref->cpu_addr);
+		errors = true;
+
+	}
+
+	/*
+	 * This may be no bug in reality - but most implementations of the
+	 * DMA API don't handle this properly, so check for it here
+	 */
+	if (ref->direction != entry->direction) {
+		dev_printk(KERN_ERR, ref->dev, "PCI-DMA: device driver frees "
+			   "DMA memory with different direction "
+			   "[device address=0x%016llx] [size=%llu bytes] "
+			   "[mapped with %s] [unmapped with %s]\n",
+			   ref->dev_addr, ref->size,
+			   dir2name[entry->direction],
+			   dir2name[ref->direction]);
+		errors = true;
+	}
+
+	if (errors)
+		dump_stack();
+
+	return true;
+}
+
+static void check_sync(struct device *dev, dma_addr_t addr,
+		u64 size, u64 offset, int direction, bool to_cpu)
+{
+	bool error = false;
+	unsigned long flags;
+	struct dma_debug_entry ref = {
+		.dev = dev,
+		.dev_addr = addr,
+		.size = size,
+		.direction = direction,
+	};
+	struct dma_debug_entry *entry;
+
+	spin_lock_irqsave(&dma_lock, flags);
+
+	entry = find_dma_entry(&ref);
+
+	if (!entry) {
+		dev_printk(KERN_ERR, dev, "PCI-DMA: device driver tries "
+			   "to sync DMA memory it has not allocated "
+			   "[device address=0x%016llx] [size=%llu bytes]\n",
+			   addr, size);
+		error = true;
+		goto out;
+	}
+
+	if ((offset + size) > entry->size) {
+		dev_printk(KERN_ERR, dev, "PCI-DMA: device driver syncs"
+			   " DMA memory outside allocated range "
+			   "[device address=0x%016llx] "
+			   "[allocation size=%llu bytes] [sync offset=%llu] "
+			   "[sync size=%llu]\n", entry->dev_addr, entry->size,
+			   offset, size);
+		error = true;
+	}
+
+	if (direction != entry->direction) {
+		dev_printk(KERN_ERR, dev, "PCI-DMA: device driver syncs "
+			   "DMA memory with different direction "
+			   "[device address=0x%016llx] [size=%llu bytes] "
+			   "[mapped with %s] [synced with %s]\n",
+			   addr, entry->size,
+			   dir2name[entry->direction],
+			   dir2name[direction]);
+		error = true;
+	}
+
+out:
+	spin_unlock_irqrestore(&dma_lock, flags);
+
+	if (error)
+		dump_stack();
+}
+
+
 void dma_debug_init(void)
 {
 	int i;
-- 
1.5.6.4



  parent reply	other threads:[~2008-11-21 16:30 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-11-21 16:26 [PATCH 0/10] DMA-API debugging facility Joerg Roedel
2008-11-21 16:26 ` [PATCH 01/10] x86: add Kconfig entry for DMA-API debugging Joerg Roedel
2008-11-21 16:40   ` Ingo Molnar
2008-11-21 16:48     ` Joerg Roedel
2008-11-21 23:18   ` David Miller
2008-11-21 16:26 ` [PATCH 02/10] x86: add data structures " Joerg Roedel
2008-11-21 16:42   ` Ingo Molnar
2008-11-21 16:49     ` Joerg Roedel
2008-11-21 16:26 ` [PATCH 03/10] x86: add initialization code " Joerg Roedel
2008-11-21 16:56   ` Ingo Molnar
2008-11-21 17:10     ` Joerg Roedel
2008-11-21 17:19       ` Ingo Molnar
2008-11-21 17:27         ` Ingo Molnar
2008-11-21 17:43   ` Ingo Molnar
2008-11-22  9:48     ` Joerg Roedel
2008-11-23 11:28       ` Ingo Molnar
2008-11-23 11:35         ` Ingo Molnar
2008-11-23 13:04         ` Ingo Molnar
2008-11-22  3:27   ` FUJITA Tomonori
2008-11-22  9:40     ` Joerg Roedel
2008-11-22 10:16       ` FUJITA Tomonori
2008-11-22 12:28         ` FUJITA Tomonori
2008-11-23 19:36   ` Andi Kleen
2008-11-21 16:26 ` Joerg Roedel [this message]
2008-11-21 17:07   ` [PATCH 04/10] x86: add helper functions for consistency checks Ingo Molnar
2008-11-21 16:26 ` [PATCH 05/10] x86: add check code for map/unmap_single code Joerg Roedel
2008-11-22  3:27   ` FUJITA Tomonori
2008-11-22  9:39     ` Joerg Roedel
2008-11-21 16:26 ` [PATCH 06/10] x86: add check code for map/unmap_sg code Joerg Roedel
2008-11-21 16:58   ` Ingo Molnar
2008-11-21 17:10   ` Ingo Molnar
2008-11-21 16:26 ` [PATCH 07/10] x86: add checks for alloc/free_coherent code Joerg Roedel
2008-11-21 16:57   ` Ingo Molnar
2008-11-22  3:27   ` FUJITA Tomonori
2008-11-22  9:38     ` Joerg Roedel
2008-11-21 16:26 ` [PATCH 08/10] x86: add checks for sync_single* code Joerg Roedel
2008-11-21 16:26 ` [PATCH 09/10] x86: add checks for sync_single_range* code Joerg Roedel
2008-11-21 16:26 ` [PATCH 10/10] x86: add checks for sync_sg* code Joerg Roedel
2008-11-21 16:59   ` Ingo Molnar
2008-11-21 16:37 ` [PATCH 0/10] DMA-API debugging facility Ingo Molnar
2008-11-21 16:40   ` Joerg Roedel
2008-11-21 16:52 ` Joerg Roedel
2008-11-21 16:54 ` David Woodhouse
2008-11-21 16:57   ` Joerg Roedel
2008-11-21 17:03     ` Ingo Molnar
2008-11-21 17:06     ` David Woodhouse
2008-11-21 17:18       ` Ingo Molnar
2008-11-21 17:20         ` Joerg Roedel
2008-11-21 17:24           ` David Woodhouse
2008-11-21 17:27             ` Joerg Roedel
2008-11-21 17:45               ` Ingo Molnar
2008-11-22  3:27                 ` FUJITA Tomonori
2008-11-22  9:33                   ` Joerg Roedel
2008-11-22 10:16                     ` FUJITA Tomonori
2009-02-05 22:44               ` David Woodhouse
2009-02-25  8:11                 ` David Woodhouse
2009-07-01 13:19                 ` [PATCH] Support DMA-API debugging facility on PowerPC David Woodhouse
2009-07-01 14:00                   ` Christoph Hellwig
2009-07-01 18:34                   ` Joerg Roedel
2009-07-02 14:09                   ` Kumar Gala
2008-11-21 17:22         ` [PATCH 0/10] DMA-API debugging facility David Woodhouse
2008-11-22  3:27 ` FUJITA Tomonori
2008-11-22  9:29   ` Joerg Roedel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1227284770-19215-5-git-send-email-joerg.roedel@amd.com \
    --to=joerg.roedel@amd.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=netdev@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®