linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Joerg Roedel <joerg.roedel@amd.com>
To: Ingo Molnar <mingo@redhat.com>
Cc: linux-kernel@vger.kernel.org, iommu@lists.linux-foundation.org,
	Joerg Roedel <joerg.roedel@amd.com>
Subject: [PATCH 2/3] dma-debug: add a check dma memory leaks
Date: Mon, 16 Mar 2009 18:05:29 +0100	[thread overview]
Message-ID: <1237223130-26519-3-git-send-email-joerg.roedel@amd.com> (raw)
In-Reply-To: <1237223130-26519-1-git-send-email-joerg.roedel@amd.com>

Impact: allow architectures to monitor busses for dma mem leakage

This patch adds checking code to detect if a device has pending DMA
operations when it is about to be unbound from its device driver.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
---
 include/linux/dma-debug.h |    7 +++++
 lib/dma-debug.c           |   55 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/include/linux/dma-debug.h b/include/linux/dma-debug.h
index 46a11c1..e851d23 100644
--- a/include/linux/dma-debug.h
+++ b/include/linux/dma-debug.h
@@ -24,9 +24,12 @@
 
 struct device;
 struct scatterlist;
+struct bus_type;
 
 #ifdef CONFIG_DMA_API_DEBUG
 
+extern void dma_debug_add_bus(struct bus_type *bus);
+
 extern void dma_debug_init(u32 num_entries);
 
 extern void debug_dma_map_page(struct device *dev, struct page *page,
@@ -80,6 +83,10 @@ extern void debug_dma_dump_mappings(struct device *dev);
 
 #else /* CONFIG_DMA_API_DEBUG */
 
+void dma_debug_add_bus(struct bus_type *bus)
+{
+}
+
 static inline void dma_debug_init(u32 num_entries)
 {
 }
diff --git a/lib/dma-debug.c b/lib/dma-debug.c
index b6a303f..3b20ae9 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -400,6 +400,61 @@ out_err:
 	return -ENOMEM;
 }
 
+static int device_dma_allocations(struct device *dev)
+{
+	struct dma_debug_entry *entry;
+	unsigned long flags;
+	int count = 0, i;
+
+	for (i = 0; i < HASH_SIZE; ++i) {
+		spin_lock_irqsave(&dma_entry_hash[i].lock, flags);
+		list_for_each_entry(entry, &dma_entry_hash[i].list, list) {
+			if (entry->dev == dev)
+				count += 1;
+		}
+		spin_unlock_irqrestore(&dma_entry_hash[i].lock, flags);
+	}
+
+	return count;
+}
+
+static int dma_debug_device_change(struct notifier_block *nb,
+				    unsigned long action, void *data)
+{
+	struct device *dev = data;
+	int count;
+
+
+	switch (action) {
+	case BUS_NOTIFY_UNBIND_DRIVER:
+		count = device_dma_allocations(dev);
+		if (count == 0)
+			break;
+		err_printk(dev, NULL, "DMA-API: device driver has pending "
+				"DMA allocations while released from device "
+				"[count=%d]\n", count);
+		break;
+	default:
+		break;
+	}
+
+	return 0;
+}
+
+void dma_debug_add_bus(struct bus_type *bus)
+{
+	struct notifier_block *nb;
+
+	nb = kzalloc(sizeof(struct notifier_block), GFP_KERNEL);
+	if (nb == NULL) {
+		printk(KERN_ERR "dma_debug_add_bus: out of memory\n");
+		return;
+	}
+
+	nb->notifier_call = dma_debug_device_change;
+
+	bus_register_notifier(bus, nb);
+}
 
 /*
  * Let the architectures decide how many entries should be preallocated.
-- 
1.5.6.4



  parent reply	other threads:[~2009-03-16 17:06 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-16 17:05 [PATCH 0/3] dma-debug: add additional checks Joerg Roedel
2009-03-16 17:05 ` [PATCH 1/3] dma-debug: add checks for kernel text and rodata Joerg Roedel
2009-03-16 17:05 ` Joerg Roedel [this message]
2009-03-16 17:05 ` [PATCH 3/3] dma-debug/x86: register pci bus for dma-debug leak detection Joerg Roedel
2009-03-17 12:01 ` [PATCH 0/3] dma-debug: add additional checks Joerg Roedel
2009-03-18  9:38   ` Ingo Molnar
2009-03-18 11:20     ` forcedeth 0000:00:0a.0: DMA-API: device driver frees DMA memory with wrong function [device address=0x0000000035992232] [size=42 bytes] [mapped as single] [unmapped as page] Ingo Molnar
2009-03-18 11:23     ` [PATCH 0/3] dma-debug: add additional checks Ingo Molnar
2009-03-18 11:38       ` Peter Zijlstra
2009-03-18 11:57         ` [tip:core/locking] lockdep: add stack dumps to asserts Peter Zijlstra
2009-03-18 12:19         ` [PATCH 0/3] dma-debug: add additional checks Joerg Roedel
2009-03-18 12:28           ` Peter Zijlstra
2009-03-18 12:45             ` Ingo Molnar
2009-03-31 12:57         ` [tip:core/locking] lockdep: add stack dumps to asserts Peter Zijlstra
2009-03-18 11:28     ` e1000e 0000:04:00.0: DMA-API: device driver frees DMA memory with wrong function [device address=0x000000003e5a2c02] [size=42 bytes] [mapped as single] [unmapped as page] Ingo Molnar
2009-03-18 11:56     ` [PATCH 0/3] dma-debug: add additional checks Joerg Roedel
2009-03-18 12:04       ` e1000e 0000:00:19.0: DMA-API: device driver tries to free DMA memory it has not allocated [device address=0x0000000052ff084a] [size=90 bytes] Ingo Molnar
2009-03-18 12:54         ` Lubomir Rintel
2009-03-18 14:09           ` Ingo Molnar
2009-03-18 14:21             ` forcedeth 0000:00:0a.0: DMA-API: device driver frees DMA memory with wrong function [device address=0x0000000035992232] [size=42 bytes] [mapped as single] [unmapped as page] Ingo Molnar
2009-03-18 16:43             ` e1000e 0000:00:19.0: DMA-API: device driver tries to free DMA memory it has not allocated [device address=0x0000000052ff084a] [size=90 bytes] Brandeburg, Jesse
2009-03-18 16:47               ` Ingo Molnar
2009-03-18 17:41                 ` Brandeburg, Jesse
2009-03-18 17:53                   ` Ingo Molnar
2009-03-20 17:33             ` 3c59x 0000:00:0b.0: DMA-API: device driver maps memory from kernel text or rodata [addr=c0fffe54] [size=428] Ingo Molnar
2009-03-18 10:00   ` [tip:core/iommu] dma-debug: fix dma_debug_add_bus() definition for !CONFIG_DMA_API_DEBUG Ingo Molnar
2009-03-18 11:54     ` 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=1237223130-26519-3-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 \
    /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;
as well as URLs for NNTP newsgroup(s).